<!doctype html>














<!-- `site.alt_lang` can specify a language different from the UI -->
<html lang="en" 
  
>
  <!-- The Head -->

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="#f7f7f7">
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1b1b1e">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta
    name="viewport"
    content="width=device-width, user-scalable=no initial-scale=1, shrink-to-fit=no, viewport-fit=cover"
  >

  

  

  
    <!-- Begin Jekyll SEO tag v2.8.0 -->
<meta name="generator" content="Jekyll v4.3.2" />
<meta property="og:title" content="Customized Scheduler - google Ghost" />
<meta name="author" content="Chunsheng Luo" />
<meta property="og:locale" content="en" />
<meta name="description" content="一、鸿蒙调度/LiteOS调度" />
<meta property="og:description" content="一、鸿蒙调度/LiteOS调度" />
<link rel="canonical" href="https://luochenglcs.github.io/posts/Customized-Scheduler-In-Userspace/" />
<meta property="og:url" content="https://luochenglcs.github.io/posts/Customized-Scheduler-In-Userspace/" />
<meta property="og:site_name" content="Cheng Luo" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2021-08-22T00:00:00+00:00" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="Customized Scheduler - google Ghost" />
<meta name="twitter:site" content="@luochenglcs" />
<meta name="twitter:creator" content="@Chunsheng Luo" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"Chunsheng Luo","url":"https://github.com/luochenglcs"},"dateModified":"2023-08-03T07:26:45+00:00","datePublished":"2021-08-22T00:00:00+00:00","description":"一、鸿蒙调度/LiteOS调度","headline":"Customized Scheduler - google Ghost","mainEntityOfPage":{"@type":"WebPage","@id":"https://luochenglcs.github.io/posts/Customized-Scheduler-In-Userspace/"},"url":"https://luochenglcs.github.io/posts/Customized-Scheduler-In-Userspace/"}</script>
<!-- End Jekyll SEO tag -->

  

  <title>Customized Scheduler - google Ghost | Cheng Luo
  </title>

  <!--
  The Favicons for Web, Android, Microsoft, and iOS (iPhone and iPad) Apps
  Generated by: https://realfavicongenerator.net/
-->



<link rel="apple-touch-icon" sizes="180x180" href="/assets/img/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/img/favicons/favicon-16x16.png">
<link rel="manifest" href="/assets/img/favicons/site.webmanifest">
<link rel="shortcut icon" href="/assets/img/favicons/favicon.ico">
<meta name="apple-mobile-web-app-title" content="Cheng Luo">
<meta name="application-name" content="Cheng Luo">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="/assets/img/favicons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">


  
    
      <link rel="preconnect" href="https://fonts.googleapis.com" >
      <link rel="dns-prefetch" href="https://fonts.googleapis.com" >
    
      <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
      <link rel="dns-prefetch" href="https://fonts.gstatic.com" crossorigin>
    
      <link rel="preconnect" href="https://fonts.googleapis.com" >
      <link rel="dns-prefetch" href="https://fonts.googleapis.com" >
    
      <link rel="preconnect" href="https://cdn.jsdelivr.net" >
      <link rel="dns-prefetch" href="https://cdn.jsdelivr.net" >
    

    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lato&family=Source+Sans+Pro:wght@400;600;700;900&display=swap">
  

  <!-- GA -->
  

  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.0/css/all.min.css">

  <link rel="stylesheet" href="/assets/css/style.css">

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tocbot@4.21.0/dist/tocbot.min.css">
  

  
    <!-- Manific Popup -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/magnific-popup@1.1.0/dist/magnific-popup.min.css">
  

  <!-- JavaScript -->

  
    <!-- Switch the mode between dark and light. -->

<script type="text/javascript">
  class ModeToggle {
    static get MODE_KEY() {
      return 'mode';
    }
    static get MODE_ATTR() {
      return 'data-mode';
    }
    static get DARK_MODE() {
      return 'dark';
    }
    static get LIGHT_MODE() {
      return 'light';
    }
    static get ID() {
      return 'mode-toggle';
    }

    constructor() {
      if (this.hasMode) {
        if (this.isDarkMode) {
          if (!this.isSysDarkPrefer) {
            this.setDark();
          }
        } else {
          if (this.isSysDarkPrefer) {
            this.setLight();
          }
        }
      }

      let self = this;

      /* always follow the system prefers */
      this.sysDarkPrefers.addEventListener('change', () => {
        if (self.hasMode) {
          if (self.isDarkMode) {
            if (!self.isSysDarkPrefer) {
              self.setDark();
            }
          } else {
            if (self.isSysDarkPrefer) {
              self.setLight();
            }
          }

          self.clearMode();
        }

        self.notify();
      });
    } /* constructor() */

    get sysDarkPrefers() {
      return window.matchMedia('(prefers-color-scheme: dark)');
    }

    get isSysDarkPrefer() {
      return this.sysDarkPrefers.matches;
    }

    get isDarkMode() {
      return this.mode === ModeToggle.DARK_MODE;
    }

    get isLightMode() {
      return this.mode === ModeToggle.LIGHT_MODE;
    }

    get hasMode() {
      return this.mode != null;
    }

    get mode() {
      return sessionStorage.getItem(ModeToggle.MODE_KEY);
    }

    /* get the current mode on screen */
    get modeStatus() {
      if (this.isDarkMode || (!this.hasMode && this.isSysDarkPrefer)) {
        return ModeToggle.DARK_MODE;
      } else {
        return ModeToggle.LIGHT_MODE;
      }
    }

    setDark() {
      document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.DARK_MODE);
      sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
    }

    setLight() {
      document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.LIGHT_MODE);
      sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
    }

    clearMode() {
      document.documentElement.removeAttribute(ModeToggle.MODE_ATTR);
      sessionStorage.removeItem(ModeToggle.MODE_KEY);
    }

    /* Notify another plugins that the theme mode has changed */
    notify() {
      window.postMessage(
        {
          direction: ModeToggle.ID,
          message: this.modeStatus
        },
        '*'
      );
    }

    flipMode() {
      if (this.hasMode) {
        if (this.isSysDarkPrefer) {
          if (this.isLightMode) {
            this.clearMode();
          } else {
            this.setLight();
          }
        } else {
          if (this.isDarkMode) {
            this.clearMode();
          } else {
            this.setDark();
          }
        }
      } else {
        if (this.isSysDarkPrefer) {
          this.setLight();
        } else {
          this.setDark();
        }
      }

      this.notify();
    } /* flipMode() */
  } /* ModeToggle */

  const modeToggle = new ModeToggle();
</script>

  

  <!-- A placeholder to allow defining custom metadata -->

</head>


  <body>
    <!-- The Side Bar -->

<div id="sidebar" class="d-flex flex-column align-items-end">
  <div class="profile-wrapper">
    <a href="/" id="avatar" class="rounded-circle">
      
        
        <img src="/assets/img/favicons/android-chrome-512x512.png" width="112" height="112" alt="avatar" onerror="this.style.display='none'">
      
    </a>

    <div class="site-title">
      <a href="/">Cheng Luo</a>
    </div>
    <div class="site-subtitle fst-italic">Linux operating system engineer</div>
  </div>
  <!-- .profile-wrapper -->

  <ul class="nav flex-column flex-grow-1 w-100 ps-0">
    <!-- home -->
    <li class="nav-item">
      <a href="/" class="nav-link">
        <i class="fa-fw fas fa-home"></i>
        <span>HOME</span>
      </a>
    </li>
    <!-- the real tabs -->
    
      <li class="nav-item">
        <a href="/categories/" class="nav-link">
          <i class="fa-fw fas fa-stream"></i>
          

          <span>CATEGORIES</span>
        </a>
      </li>
      <!-- .nav-item -->
    
      <li class="nav-item">
        <a href="/tags/" class="nav-link">
          <i class="fa-fw fas fa-tags"></i>
          

          <span>TAGS</span>
        </a>
      </li>
      <!-- .nav-item -->
    
      <li class="nav-item">
        <a href="/archives/" class="nav-link">
          <i class="fa-fw fas fa-archive"></i>
          

          <span>ARCHIVES</span>
        </a>
      </li>
      <!-- .nav-item -->
    
      <li class="nav-item">
        <a href="/about/" class="nav-link">
          <i class="fa-fw fas fa-info-circle"></i>
          

          <span>ABOUT</span>
        </a>
      </li>
      <!-- .nav-item -->
    
  </ul>
  <!-- ul.nav.flex-column -->

  <div class="sidebar-bottom d-flex flex-wrap  align-items-center w-100">
    
      <button class="mode-toggle btn" aria-label="Switch Mode">
        <i class="fas fa-adjust"></i>
      </button>

      
        <span class="icon-border"></span>
      
    

    
      

      
        <a
          href="https://github.com/luochenglcs"
          aria-label="github"
          

          
            target="_blank"
            
          

          

          
            rel="noopener noreferrer"
          
        >
          <i class="fab fa-github"></i>
        </a>
      
    
      

      
    
      

      
        <a
          href="javascript:location.href = 'mailto:' + ['luochunsheng','ustc.edu'].join('@')"
          aria-label="email"
          

          

          

          
        >
          <i class="fas fa-envelope"></i>
        </a>
      
    
  </div>
  <!-- .sidebar-bottom -->
</div>
<!-- #sidebar -->


    <div id="main-wrapper" class="d-flex justify-content-center">
      <div id="main" class="container px-xxl-5">
        <!-- The Top Bar -->

<div id="topbar-wrapper">
  <div
    id="topbar"
    class="container d-flex align-items-center justify-content-between h-100"
  >
    <span id="breadcrumb">
      

      
        
          
            <span>
              <a href="/">
                Home
              </a>
            </span>

          
        
          
        
          
            
              <span>Customized Scheduler - google Ghost</span>
            

          
        
      
    </span>
    <!-- endof #breadcrumb -->

    <i id="sidebar-trigger" class="fas fa-bars fa-fw"></i>

    <div id="topbar-title">
      Post
    </div>

    <i id="search-trigger" class="fas fa-search fa-fw"></i>
    <span id="search-wrapper" class="align-items-center">
      <i class="fas fa-search fa-fw"></i>
      <input
        class="form-control"
        id="search-input"
        type="search"
        aria-label="search"
        autocomplete="off"
        placeholder="Search..."
      >
    </span>
    <span id="search-cancel">Cancel</span>
  </div>
</div>

        











<div class="row">
  <!-- core -->
  <div id="core-wrapper" class="col-12 col-lg-11 col-xl-9 pe-xl-4">
    

    <div class="post px-1 px-md-2">
      

      
        
      
        <!-- Refactor the HTML structure -->



<!--
  In order to allow a wide table to scroll horizontally,
  we suround the markdown table with `<div class="table-wrapper">` and `</div>`
-->



<!--
  Fixed kramdown code highlight rendering:
  https://github.com/penibelst/jekyll-compress-html/issues/101
  https://github.com/penibelst/jekyll-compress-html/issues/71#issuecomment-188144901
-->



<!-- Change the icon of checkbox -->


<!-- images -->



  
  

  <!-- CDN URL -->
  

  <!-- Add image path -->
  

  
    
      
      
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  
    

    
    

    

    
    

    
    
    
    

    
      

      
      
      

      
    
      

      
      
      

      
    

    <!-- take out classes -->
    

    

    

    

    <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
    
    

    <!-- add image placeholder -->
    
      
    

    <!-- Bypass the HTML-proofer test -->
    

    
      <!-- make sure the `<img>` is wrapped by `<a>` -->
      

      
        <!-- create the image wrapper -->
        
      
    

    <!-- combine -->
    

  

  



<!-- Add header for code snippets -->



<!-- Create heading anchors -->





  
  

  

  
  

  
    
    

    
      
        
        
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    

    

  

  
  

  
    
    

    
      
        
        
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    

    

  

  
  

  
    
    

    
      
        
        
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    
      

      
      

      
      
      

      

    

    

  




<!-- return -->




<h1 data-toc-skip>Customized Scheduler - google Ghost</h1>

<div class="post-meta text-muted">
    <!-- published date -->
    <span>
      Posted
      <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class=""
  data-ts="1629590400"
  data-df="ll"
  
    data-bs-toggle="tooltip" data-bs-placement="bottom"
  
>
  Aug 22, 2021
</em>

    </span>

    <!-- lastmod date -->
    
    <span>
      Updated
      <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class=""
  data-ts="1691047605"
  data-df="ll"
  
    data-bs-toggle="tooltip" data-bs-placement="bottom"
  
>
  Aug  3, 2023
</em>

    </span>
    

  

  <div class="d-flex justify-content-between">
    <!-- author(s) -->
    <span>
      

      By

      <em>
      
        
          <a href="https://github.com/luochenglcs">Chunsheng Luo</a>
          
        
      
      </em>
    </span>

    <div>
      <!-- read time -->
      <!-- Calculate the post's reading time, and display the word count in tooltip -->



<!-- words per minute -->










<!-- return element -->
<span
  class="readtime"
  data-bs-toggle="tooltip"
  data-bs-placement="bottom"
  title="7189 words"
>
  <em>39 min</em> read</span>

    </div>

  </div> <!-- .d-flex -->

</div> <!-- .post-meta -->
<div class="post-content">
  <h3 id="一鸿蒙调度liteos调度"><span class="me-2">一、鸿蒙调度/LiteOS调度</span><a href="#一鸿蒙调度liteos调度" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">//鸿蒙OS调度</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//codechina.csdn.net/kuangyufei/kernel_liteos_a_note/-/wikis/04_%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E7%AF%87</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//juejin.cn/post/7015965298234753032</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="rouge-code"><pre><span class="err">线程状态说明：</span>
<span class="err">初始化（</span><span class="n">Init</span><span class="err">）：该线程正在被创建。</span>
<span class="err">就绪（</span><span class="n">Ready</span><span class="err">）：该线程在就绪列表中，等待</span><span class="n">CPU</span><span class="err">调度。</span>
<span class="err">运行（</span><span class="n">Running</span><span class="err">）：该线程正在运行。</span>
<span class="err">阻塞（</span><span class="n">Blocked</span><span class="err">）：该线程被阻塞挂起。</span><span class="n">Blocked</span><span class="err">状态包括：</span><span class="n">pend</span><span class="p">(</span><span class="err">因为锁、事件、信号量等阻塞</span><span class="p">)</span><span class="err">、</span><span class="n">suspend</span><span class="err">（主动</span><span class="n">pend</span><span class="err">）、</span><span class="n">delay</span><span class="p">(</span><span class="err">延时阻塞</span><span class="p">)</span><span class="err">、</span><span class="n">pendtime</span><span class="p">(</span><span class="err">因为锁、事件、信号量时间等超时等待</span><span class="p">)</span><span class="err">。</span>
<span class="err">退出（</span><span class="n">Exit</span><span class="err">）：该线程运行结束，等待父线程回收其控制块资源。</span>
<span class="err">说</span><span class="n">LosTaskCB</span><span class="err">之前先说下官方文档任务状态对应的</span> <span class="n">define</span><span class="err">，可以看出</span><span class="n">task</span><span class="err">和线程是一个东西。</span>
<span class="cp">#define OS_TASK_STATUS_INIT         0x0001U
#define OS_TASK_STATUS_READY        0x0002U
#define OS_TASK_STATUS_RUNNING      0x0004U
#define OS_TASK_STATUS_SUSPEND      0x0008U
#define OS_TASK_STATUS_PEND         0x0010U
#define OS_TASK_STATUS_DELAY        0x0020U
#define OS_TASK_STATUS_TIMEOUT      0x0040U
#define OS_TASK_STATUS_PEND_TIME    0x0080U
#define OS_TASK_STATUS_EXIT         0x0100U
</span><span class="o">-----</span><span class="n">https</span><span class="o">:</span><span class="c1">//juejin.cn/post/7015965298234753032</span>
</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转移.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转移.png" alt="image-20211028145444024" class="lazyload" data-proofer-ignore></a></p>

<p>Huawei LiteOS 系统中的任务管理模块为用户提供下面几种功能。</p>

<div class="table-wrapper"><table>
  <thead>
    <tr>
      <th>功能分类</th>
      <th>接口名</th>
      <th>描述</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>任务的创建和删除</td>
      <td>LOS_TaskCreateOnly</td>
      <td>创建任务，并使该任务进入suspend状态，并不调度。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskCreate</td>
      <td>创建任务，并使该任务进入ready状态，并调度。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskDelete</td>
      <td>删除指定的任务。</td>
    </tr>
    <tr>
      <td>任务状态控制</td>
      <td>LOS_TaskResume</td>
      <td>恢复挂起的任务。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskSuspend</td>
      <td>挂起指定的任务。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskDelay</td>
      <td>任务延时等待。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskYield</td>
      <td>显式放权，调整指定优先级的任务调度顺序。</td>
    </tr>
    <tr>
      <td>任务调度的控制</td>
      <td>LOS_TaskLock</td>
      <td>锁任务调度。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskUnlock</td>
      <td>解锁任务调度。</td>
    </tr>
    <tr>
      <td>任务优先级的控制</td>
      <td>LOS_CurTaskPriSet</td>
      <td>设置当前任务的优先级。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskPriSet</td>
      <td>设置指定任务的优先级。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskPriGet</td>
      <td>获取指定任务的优先级。</td>
    </tr>
    <tr>
      <td>任务信息获取</td>
      <td>LOS_CurTaskIDGet</td>
      <td>获取当前任务的ID。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskInfoGet</td>
      <td>设置指定任务的优先级。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskPriGet</td>
      <td>获取指定任务的信息。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskStatusGet</td>
      <td>获取指定任务的状态。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskNameGet</td>
      <td>获取指定任务的名称。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_TaskInfoMonitor</td>
      <td>监控所有任务，获取所有任务的信息。</td>
    </tr>
    <tr>
      <td> </td>
      <td>LOS_NextTaskIDGet</td>
      <td>获取即将被调度的任务的ID。</td>
    </tr>
  </tbody>
</table></div>

<h3 id="二ghost调度器"><span class="me-2">二、ghost调度器</span><a href="#二ghost调度器" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>
<p>在userspace设计调度代理agent， 内核将线程信息（new、wakeup、block、relax、dead等）通过共享内存方式发送给调度代理agent，调度代理agent实施调度决策，并通过Syscall下发给内核;</p>

<p>ghost调度实现： ghost-kenrel / ghost-userspace</p>

<h4 id="1-ghost-kernel如何调度切换"><span class="me-2">1&gt; ghost-kernel–如何调度切换</span><a href="#1-ghost-kernel如何调度切换" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>
<h5 id="1ghost-class-timerfd"><span class="me-2">1、ghost class timerfd</span><a href="#1ghost-class-timerfd" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="n">do_timerfd_settime</span> <span class="p">{</span>
    <span class="p">...</span>
    <span class="cp">#ifdef CONFIG_SCHED_CLASS_GHOST
</span>       <span class="k">if</span> <span class="p">(</span><span class="n">tfdl</span><span class="p">)</span>
               <span class="n">memcpy</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ctx</span><span class="o">-&gt;</span><span class="n">timerfd_ghost</span><span class="p">,</span> <span class="n">tfdl</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">timerfd_ghost</span><span class="p">));</span>
       <span class="k">else</span>
               <span class="n">ctx</span><span class="o">-&gt;</span><span class="n">timerfd_ghost</span><span class="p">.</span><span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>   <span class="cm">/* disabled */</span>
	<span class="cp">#endif
</span>    <span class="p">...</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></div></div>

<h5 id="2kernelschedghostc"><span class="me-2">2、kernel/sched/ghost.c</span><a href="#2kernelschedghostc" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<p>​	<strong>1&gt; 新增调度类 调度实体….</strong></p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="rouge-code"><pre><span class="c1">//新增ghost task和agent的调度类</span>
<span class="cp">#define SCHED_DATA                             \
        STRUCT_ALIGN();                         \
        __begin_sched_classes = .;              \
        *(__idle_sched_class)                   \
+       *(__ghost_sched_class)                  \ //ghost task schedhere
</span>        <span class="o">*</span><span class="p">(</span><span class="n">__fair_sched_class</span><span class="p">)</span>                   \
        <span class="o">*</span><span class="p">(</span><span class="n">_rt_sched_class</span><span class="p">)</span>                     \
        <span class="o">*</span><span class="p">(</span><span class="n">__dl_sched_class</span><span class="p">)</span>                     \
<span class="o">+</span>       <span class="o">*</span><span class="p">(</span><span class="n">__ghost_agent_sched_class</span><span class="p">)</span>            <span class="err">\</span>  <span class="c1">///ghost agent sched here</span>
        <span class="o">*</span><span class="p">(</span><span class="n">__stop_sched_class</span><span class="p">)</span>                   \
        <span class="n">__end_sched_classes</span> <span class="o">=</span> <span class="p">.;</span>

</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="rouge-code"><pre><span class="c1">//ghost调度实体阿</span>
<span class="o">+</span><span class="k">struct</span> <span class="n">sched_ghost_entity</span> <span class="p">{</span>
<span class="o">+</span>       <span class="k">struct</span> <span class="n">list_head</span> <span class="n">run_list</span><span class="p">;</span>
<span class="o">+</span>       <span class="n">ktime_t</span> <span class="n">last_runnable_at</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>       <span class="cm">/* The following fields are protected by 'task_rq(p)-&gt;lock' */</span>
<span class="o">+</span>       <span class="k">struct</span> <span class="n">ghost_queue</span> <span class="o">*</span><span class="n">dst_q</span><span class="p">;</span>
<span class="o">+</span>       <span class="k">struct</span> <span class="n">ghost_status_word</span> <span class="o">*</span><span class="n">status_word</span><span class="p">;</span>
<span class="o">+</span>       <span class="k">struct</span> <span class="n">ghost_enclave</span> <span class="o">*</span><span class="n">enclave</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>       <span class="cm">/*
+        * See also ghost_prepare_task_switch() and ghost_deferred_msgs()
+        * for flags that are used to defer messages.
+        */</span>
<span class="o">+</span>       <span class="n">uint</span> <span class="n">blocked_task</span> <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
<span class="o">+</span>       <span class="n">uint</span> <span class="n">yield_task</span> <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
<span class="o">+</span>       <span class="n">uint</span> <span class="n">new_task</span>   <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
<span class="o">+</span>       <span class="n">uint</span> <span class="n">agent</span>      <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>       <span class="k">struct</span> <span class="n">list_head</span> <span class="n">task_list</span><span class="p">;</span>
<span class="o">+</span><span class="p">};</span>

</pre></td></tr></tbody></table></code></div></div>

<p>​	<strong>2&gt; scheduler_ipi</strong></p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="rouge-code"><pre><span class="c1">//核间中断处理函数</span>
<span class="n">do_handle_IPI</span>
    <span class="o">-&gt;</span><span class="n">scheduler_ipi</span> <span class="c1">//IPI_RESCHEDULE</span>
    
<span class="kt">void</span> <span class="nf">scheduler_ipi</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
	<span class="cm">/*
	 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
	 * TIF_NEED_RESCHED remotely (for the first time) will also send
	 * this IPI.
	 */</span>
	<span class="n">preempt_fold_need_resched</span><span class="p">();</span>

	<span class="k">if</span> <span class="p">(</span><span class="n">llist_empty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">this_rq</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">wake_list</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">got_nohz_idle_kick</span><span class="p">())</span>
		<span class="k">return</span><span class="p">;</span>

	<span class="cm">/*
	 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
	 * traditionally all their work was done from the interrupt return
	 * path. Now that we actually do some work, we need to make sure
	 * we do call them.
	 *
	 * Some archs already do call them, luckily irq_enter/exit nest
	 * properly.
	 *
	 * Arguably we should visit all archs and update all handlers,
	 * however a fair share of IPIs are still resched only so this would
	 * somewhat pessimize the simple resched case.
	 */</span>
	<span class="n">irq_enter</span><span class="p">();</span>
	<span class="n">sched_ttwu_pending</span><span class="p">();</span>

	<span class="cm">/*
	 * Check if someone kicked us for doing the nohz idle load balance.
	 */</span>
	<span class="k">if</span> <span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="n">got_nohz_idle_kick</span><span class="p">()))</span> <span class="p">{</span>
		<span class="n">this_rq</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">idle_balance</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
		<span class="n">raise_softirq_irqoff</span><span class="p">(</span><span class="n">SCHED_SOFTIRQ</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">irq_exit</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">//https://www.codenong.com/cs106431107/ 引用于</span>
<span class="n">scheduler_ipi</span><span class="p">()</span><span class="err">函数调用</span><span class="n">sched_ttwu_pending</span><span class="p">()</span><span class="err">函数唤醒</span><span class="n">pending</span><span class="err">的任务然后调用</span>
<span class="n">raise_softirq_irqoff</span><span class="err">（）函数发起一个软中断，软中断将在后续的文章中介绍。</span>

<span class="n">IPI_CALL_FUNC</span><span class="err">：函数</span><span class="n">smp_call_function</span><span class="p">()</span><span class="err">生成的中断，通过调用函数</span>
<span class="n">generic_smp_call_function_interrupt</span><span class="p">()</span><span class="err">最终调用函数</span><span class="n">flush_smp_call_function_queue</span><span class="err">（），</span>
<span class="err">该函数调用所有在队列中</span><span class="n">pending</span><span class="err">的回调函数。</span><span class="n">flush_smp_call_function_queue</span><span class="err">（）函数的源码可</span>
<span class="err">以在</span><span class="n">kernel</span><span class="o">/</span><span class="n">smp</span><span class="p">.</span><span class="n">c</span><span class="err">文件中可以找到：</span>
    
 <span class="n">http</span><span class="o">:</span><span class="c1">//oliveryang.net/2016/03/linux-scheduler-2/</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="c1">//流程1</span>
<span class="n">ghost_latched_task_preempted</span>
    <span class="o">-&gt;</span><span class="n">_ghost_task_preempted</span>  <span class="k">if</span> <span class="n">ghost_police</span>
        <span class="o">-&gt;</span><span class="n">set_tsk_need_resched</span> <span class="o">&amp;&amp;</span> <span class="n">set_preempt_need_resched</span>
        
<span class="c1">//流程2 </span>
<span class="n">context_switch</span>
    <span class="o">-&gt;</span><span class="n">prepare_task_switch</span> 
        <span class="o">-&gt;</span><span class="n">ghost_prepare_task_switch</span> <span class="o">||</span> <span class="n">pick_next_ghost_agent</span> <span class="c1">//pick_next_ghost_agent忽略</span>
            <span class="o">-&gt;</span><span class="n">ghost_task_preempted</span>
                <span class="o">-&gt;</span><span class="n">_ghost_task_preempted</span> 
</pre></td></tr></tbody></table></code></div></div>

<h5 id="3new-syscall--ghost-ghost_run"><span class="me-2">3、new syscall – ghost ghost_run</span><a href="#3new-syscall--ghost-ghost_run" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="mi">450</span>    <span class="mi">64</span>      <span class="n">ghost_run</span>               <span class="n">sys_ghost_run</span>
<span class="mi">451</span>    <span class="mi">64</span>      <span class="n">ghost</span>                   <span class="n">sys_ghost</span>
</pre></td></tr></tbody></table></code></div></div>

<h5 id="4bpf"><span class="me-2">4、bpf</span><a href="#4bpf" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="rouge-code"><pre><span class="c1">//include/linux/bpf_types.h</span>
<span class="n">BPF_PROG_TYPE</span><span class="p">(</span><span class="n">BPF_PROG_TYPE_GHOST_SCHED</span><span class="p">,</span> <span class="n">ghost_sched</span><span class="p">,</span> <span class="k">struct</span> <span class="n">bpf_ghost_sched</span><span class="p">,</span>
              <span class="k">struct</span> <span class="n">bpf_ghost_sched_kern</span><span class="p">)</span>

<span class="c1">//kernel/sched/bpf.c</span>
<span class="n">BPF_CALL_2</span><span class="p">(</span><span class="n">bpf_ghost_wake_agent</span><span class="p">,</span> <span class="k">struct</span> <span class="n">bpf_ghost_sched_kern</span> <span class="o">*</span><span class="p">,</span> <span class="n">ctx</span><span class="p">,</span> <span class="n">u32</span><span class="p">,</span> <span class="n">cpu</span><span class="p">)</span>
<span class="p">{</span>
        <span class="k">return</span> <span class="n">ghost_wake_agent_on_check</span><span class="p">(</span><span class="n">cpu</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">//</span>
<span class="n">BPF_CALL_4</span><span class="p">(</span><span class="n">bpf_ghost_run_gtid</span><span class="p">,</span> <span class="k">struct</span> <span class="n">bpf_ghost_sched_kern</span> <span class="o">*</span><span class="p">,</span> <span class="n">ctx</span><span class="p">,</span> <span class="n">s64</span><span class="p">,</span> <span class="n">gtid</span><span class="p">,</span>
           <span class="n">u32</span><span class="p">,</span> <span class="n">task_barrier</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="n">run_flags</span><span class="p">)</span>
<span class="p">{</span>
        <span class="k">return</span> <span class="n">ghost_run_gtid_on</span><span class="p">(</span><span class="n">gtid</span><span class="p">,</span> <span class="n">task_barrier</span><span class="p">,</span> <span class="n">run_flags</span><span class="p">,</span>
                                 <span class="n">smp_processor_id</span><span class="p">());</span>
<span class="p">}</span>
<span class="c1">//</span>
<span class="k">static</span> <span class="k">const</span> <span class="k">struct</span> <span class="n">bpf_link_ops</span> <span class="n">bpf_ghost_sched_link_ops</span> <span class="o">=</span> <span class="p">{</span>
        <span class="p">.</span><span class="n">release</span> <span class="o">=</span> <span class="n">bpf_ghost_sched_link_release</span><span class="p">,</span>
        <span class="p">.</span><span class="n">dealloc</span> <span class="o">=</span> <span class="n">bpf_ghost_sched_link_dealloc</span><span class="p">,</span>
<span class="p">};</span>

</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/epbf-enhace-task-switch.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/epbf-enhace-task-switch.png" alt="image-20211130164919881" class="lazyload" data-proofer-ignore></a></p>

<h5 id="5kernelschedghostfsc"><span class="me-2">5、kernel/sched/ghostfs.c</span><a href="#5kernelschedghostfsc" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<h6 id="1-提供enclave的创建"><strong>1 提供enclave的创建</strong></h6>
<p>enclave机制
简介
传统用法
ghost Enclave</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">//kernel/sched/ghostfs.c</span>
<span class="n">ghostfs_init</span>
    <span class="o">-&gt;|</span> <span class="n">ghost_setup_root</span>
    	<span class="o">-&gt;|</span> <span class="n">kernfs_create_root</span> <span class="c1">//</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//www.binss.me/blog/sysfs-udev-and-Linux-Unified-Device-Model/ //kernfs-&gt;sysfs</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">//ghost enclave </span>
<span class="n">mount</span> <span class="o">-</span><span class="n">t</span> <span class="n">ghost</span> <span class="o">/</span><span class="n">dev</span><span class="o">/</span><span class="n">ghost</span> <span class="o">/</span><span class="n">sys</span><span class="o">/</span><span class="n">fs</span><span class="o">/</span><span class="n">ghost</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="mi">1</span><span class="err">、先执行</span><span class="n">agent</span><span class="o">-</span><span class="n">shinjuku</span>
<span class="mi">2</span><span class="err">、执行用例</span>
</pre></td></tr></tbody></table></code></div></div>

<h6 id="2-提供cpu_data"><strong>2 提供cpu_data</strong></h6>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="o">/</span><span class="n">sys</span><span class="o">/</span><span class="n">fs</span><span class="o">/</span><span class="n">ghost</span><span class="o">/</span><span class="n">enclave_1</span><span class="o">/</span><span class="n">cpu_data</span>
<span class="err">设置</span><span class="n">cpu</span><span class="o">-&gt;</span><span class="n">rq</span><span class="o">-&gt;</span><span class="n">ghost_rq</span><span class="o">-&gt;</span><span class="n">latched_task</span><span class="err">，提交该</span><span class="n">cpu</span><span class="err">锁定的</span><span class="n">task</span><span class="err">。</span>
<span class="err">方便</span><span class="n">ghost</span><span class="o">-</span><span class="n">userspace</span><span class="err">在该</span><span class="n">cpu</span><span class="err">上执行指定</span><span class="n">task</span><span class="p">.</span>
<span class="o">**</span><span class="err">方法</span><span class="o">**:</span> <span class="err">与用户态共享同一块物理内存</span>
</pre></td></tr></tbody></table></code></div></div>

<h6 id="3-提供status_word">3 提供status_word</h6>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="o">/</span><span class="n">sys</span><span class="o">/</span><span class="n">fs</span><span class="o">/</span><span class="n">ghost</span><span class="o">/</span><span class="n">enclave_1</span><span class="o">/</span><span class="n">sw_regions</span><span class="o">/</span><span class="n">sw_0</span>
<span class="err">进程的</span><span class="n">task</span><span class="o">-&gt;</span><span class="n">ghost</span><span class="o">-&gt;</span><span class="n">status_word</span><span class="err">从这片内存申请，记录进程的执行时间相关信息；</span>
<span class="err">方便</span><span class="n">ghost</span><span class="o">-</span><span class="n">userspace</span><span class="err">获取进程执行时间。</span>
<span class="o">**</span><span class="err">方法</span><span class="o">**</span><span class="err">：与用户态共享一块物理内存</span>
 <span class="err">每次</span><span class="n">tick</span><span class="err">更新进程的</span><span class="n">task</span><span class="err">，在</span><span class="n">dequeue_task_ghost</span><span class="err">、</span><span class="n">put_prev_task_ghost</span><span class="err">、</span><span class="n">_ghost_task_preempted</span><span class="err">、</span><span class="n">_ghost_task_new</span><span class="err">、</span><span class="n">ghost_task_yield</span><span class="err">、</span><span class="n">ghost_switchto</span><span class="err">时，更新到</span><span class="n">task</span><span class="o">-&gt;</span><span class="n">ghost</span><span class="o">-&gt;</span><span class="n">status_word</span><span class="err">中方便用户态获取。</span>
</pre></td></tr></tbody></table></code></div></div>

<h5 id="6-ghost调度分析"><span class="me-2">6、 ghost调度分析</span><a href="#6-ghost调度分析" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="rouge-code"><pre><span class="n">_ghost_task_preempted</span> 
    <span class="o">&lt;-</span> <span class="n">ghost_latched_task_preempted</span> <span class="c1">//在ghost.latched_task但是还未pick走在cpu运行,例如</span>
    	<span class="o">&lt;-</span> <span class="n">ghost_prepare_task_switch</span> <span class="c1">//1、线程切换时，next非ghost线程</span>
    	<span class="o">&lt;-</span> <span class="n">invalidate_cached_tasks</span> <span class="c1">//2. 线程迁移到其他cpu,无效latched_task; 3.线程重新设置调度类 4. DEQUEUE_SAVE</span>
        <span class="o">&lt;-</span> <span class="n">task_tick_ghost</span> <span class="c1">//5. 被agent线程抢占</span>
     	<span class="o">&lt;-</span> <span class="n">pick_next_ghost_agent</span> <span class="c1">//5. 被agent线程抢占</span>
    	<span class="o">&lt;-</span> <span class="n">release_from_ghost</span>
    	<span class="o">&lt;-</span> <span class="n">ghost_set_pnt_state</span> <span class="c1">//6.被新任务覆盖</span>
    <span class="o">------</span>
    <span class="o">&lt;-</span> <span class="n">ghost_task_preempted</span>
    	<span class="o">&lt;-</span> <span class="n">ghost_prepare_task_switch</span> <span class="c1">//正在运行的prev被抢占</span>
    	<span class="o">&lt;-</span> <span class="n">pick_next_ghost_agent</span> <span class="c1">//prev被agent任务抢占</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">ghost_task_blocked</span>
    <span class="o">&lt;-</span> <span class="n">ghost_produce_prev_msgs</span> <span class="c1">//prev-&gt;ghost.blocked_task=true,进程切换时如果发现prev线程是Dequeue sleep，则为block状态    	</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="n">task_woken_ghost</span>
    <span class="o">&lt;-</span> <span class="n">ghost_class</span><span class="p">.</span><span class="n">task_woken</span> <span class="c1">//调度类的task_woken</span>
    	<span class="o">&lt;-</span> <span class="n">ttwu_do_wakeup</span> <span class="c1">//wake up等待任务</span>
    <span class="o">----------</span>
    	<span class="o">&lt;-</span> <span class="n">wake_up_new_task</span> <span class="c1">//wake up刚创建的任务</span>
</pre></td></tr></tbody></table></code></div></div>

<p>目前per-cpu ebpf的调度的问题：</p>

<p>1、锁</p>

<p>2、进程调度延迟</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//zhuanlan.zhihu.com/p/462728452  //测量调度延迟</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="rouge-code"><pre>     <span class="n">kworker</span><span class="o">/</span><span class="mi">1</span><span class="o">:</span><span class="mi">1</span><span class="o">-</span><span class="mi">250</span>     <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">129</span><span class="p">.</span><span class="mi">594930</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">sched_switch</span> <span class="n">prev</span> <span class="n">kworker</span><span class="o">/</span><span class="mi">1</span><span class="o">:</span><span class="mi">1</span>

     <span class="n">kworker</span><span class="o">/</span><span class="mi">1</span><span class="o">:</span><span class="mi">1</span><span class="o">-</span><span class="mi">250</span>     <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">129</span><span class="p">.</span><span class="mi">594932</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">swapper</span><span class="o">/</span><span class="mi">1</span>


        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">016</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">130</span><span class="p">.</span><span class="mi">895647</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">TASK</span> <span class="n">NEW</span> 

        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">016</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">130</span><span class="p">.</span><span class="mi">895658</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">fffe</span> <span class="n">start</span> 

        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">016</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">130</span><span class="p">.</span><span class="mi">895659</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">fffc</span> <span class="n">select</span> <span class="n">cpu</span> <span class="mi">1</span>

        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">016</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">130</span><span class="p">.</span><span class="mi">895663</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">add</span> <span class="mi">1</span> <span class="n">rq</span> <span class="n">have</span> <span class="mi">1</span> <span class="n">task</span> <span class="mi">1963</span> <span class="n">gtid</span>


          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">010</span><span class="mi">916</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">sched_switch</span> <span class="n">prev</span> <span class="n">swapper</span><span class="o">/</span><span class="mi">1</span>

          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">010</span><span class="mi">925</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">migration</span><span class="o">/</span><span class="mi">1</span>

     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">010</span><span class="mi">957</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">del</span> <span class="mi">1</span> <span class="n">rq</span> <span class="n">have</span> <span class="mi">0</span> <span class="n">task</span> <span class="mi">1963</span> <span class="n">Pid</span>

     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">010</span><span class="mi">969</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">run</span> <span class="n">cpu</span> <span class="mi">1</span> <span class="n">pid</span> <span class="mi">1963</span> <span class="n">ret</span> <span class="mi">0</span>

     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">010</span><span class="mi">973</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">sched_switch</span> <span class="n">prev</span> <span class="n">migration</span><span class="o">/</span><span class="mi">1</span>

     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">010</span><span class="mi">974</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">ghostctl</span>


              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">0131</span><span class="mi">95</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">TASK</span> <span class="n">BLOCKED</span> 

              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">013200</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">pid</span> <span class="mi">1963</span> <span class="n">runtime</span> <span class="mi">36</span><span class="n">ba0a</span> <span class="n">cpu</span> <span class="mi">1</span>

              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">013201</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">block</span> <span class="n">pid</span> <span class="mi">1963</span> <span class="n">BLOCK</span> <span class="n">but</span> <span class="n">in</span> <span class="n">CPU</span> <span class="mi">1</span>

              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">013202</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">fffc</span> <span class="n">start</span> 

              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">013202</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">fffe</span> <span class="n">set</span> <span class="n">cpu</span> <span class="mi">1</span>

              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">013207</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">sched_switch</span> <span class="n">prev</span> <span class="n">ls</span>

              <span class="n">ls</span><span class="o">-</span><span class="mi">1963</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>   <span class="mi">132</span><span class="p">.</span><span class="mo">013207</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">agent_ceph</span>
</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/ghost调度.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/ghost调度.png" alt="image-20220613150600941" class="lazyload" data-proofer-ignore></a></p>

<p>do_idle函数分析：</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//www.cnblogs.com/Linux-tech/p/13326567.html</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//www.cnblogs.com/LoyenWang/p/11379937.html</span>
</pre></td></tr></tbody></table></code></div></div>

<p>resched_cpu_unlocked:</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//zhuanlan.zhihu.com/p/500191837 IPI中断类型</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//zhuanlan.zhihu.com/p/373959024 进程切换，地址空间切换</span>
</pre></td></tr></tbody></table></code></div></div>

<p>内核主动触发调度点：</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//blog.csdn.net/pwl999/article/details/78817899 Linux schedule 1、调度的时刻</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//blog.51cto.com/qmiller/4842433 Linux内核进程调度发生的时间点</span>
</pre></td></tr></tbody></table></code></div></div>

<p>问题定位原因：</p>

<p>1、idle线程无法退出的原因：</p>

<p>​	1&gt; ipi_scheduler确实会从idle状态退出，走进schedule_idle</p>

<p>​	2&gt; schedule_idle-&gt;__schedule-&gt;pick_next_task 走进fair_class的优化分支，因为此时ghost_task还没有入队；</p>

<p>跟踪日志如下：</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="rouge-code"><pre> <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">84</span><span class="p">.</span><span class="mo">01215</span><span class="mi">9</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">swapper</span><span class="o">/</span><span class="mi">1</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">84</span><span class="p">.</span><span class="mo">012161</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">schedule_idle</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">84</span><span class="p">.</span><span class="mo">012167</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">cpu_idle</span><span class="o">:</span> <span class="n">state</span> <span class="mi">1</span> <span class="n">cpu</span> <span class="mi">1</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">01</span><span class="mi">8</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605119</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">TASK</span> <span class="n">NEW</span> <span class="c1">//ghost task 从cfs 切成ghost调度类</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">01</span><span class="mi">8</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605125</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">fffe</span> <span class="n">start</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">01</span><span class="mi">8</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605126</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">fffc</span> <span class="n">select</span> <span class="n">cpu</span> <span class="mi">1</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">01</span><span class="mi">8</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605128</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">add</span> <span class="mi">1</span> <span class="n">rq</span> <span class="n">have</span> <span class="mi">1</span> <span class="n">task</span> <span class="mi">3518</span> <span class="n">gtid</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">.</span><span class="n">h</span><span class="p">.</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605163</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">ipi_entry</span><span class="o">:</span> <span class="n">Rescheduling</span> <span class="n">interrupts</span> <span class="c1">//ipi_resche</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dNh</span><span class="p">.</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605167</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">ipi_exit</span><span class="o">:</span> <span class="n">Rescheduling</span> <span class="n">interrupts</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605169</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">cpu_idle</span><span class="o">:</span> <span class="n">state</span> <span class="n">ffffffff</span> <span class="n">cpu</span> <span class="mi">1</span> <span class="c1">//idle进程退出</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605173</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">schedule_idle</span> <span class="c1">//重新调度idle，发现无任务</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">85</span><span class="p">.</span><span class="mi">605179</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">cpu_idle</span><span class="o">:</span> <span class="n">state</span> <span class="mi">1</span> <span class="n">cpu</span> <span class="mi">1</span> <span class="c1">//重新pick idle</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">0120</span><span class="mi">86</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">cpu_idle</span><span class="o">:</span> <span class="n">state</span> <span class="n">ffffffff</span> <span class="n">cpu</span> <span class="mi">1</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">dN</span><span class="p">..</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">012121</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">schedule_idle</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">012127</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">sched_switch</span> <span class="n">prev</span> <span class="n">swapper</span><span class="o">/</span><span class="mi">1</span>
          <span class="o">&lt;</span><span class="n">idle</span><span class="o">&gt;-</span><span class="mi">0</span>       <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">01212</span><span class="mi">8</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">migration</span><span class="o">/</span><span class="mi">1</span>
     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">01213</span><span class="mi">8</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">del</span> <span class="mi">1</span> <span class="n">rq</span> <span class="n">have</span> <span class="mi">0</span> <span class="n">task</span> <span class="mi">3518</span> <span class="n">Pid</span>
     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">012147</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">run</span> <span class="n">cpu</span> <span class="mi">1</span> <span class="n">pid</span> <span class="mi">3518</span> <span class="n">ret</span> <span class="mi">0</span>
     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">01214</span><span class="mi">9</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">sched_switch</span> <span class="n">prev</span> <span class="n">migration</span><span class="o">/</span><span class="mi">1</span>
     <span class="n">migration</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">18</span>      <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">012152</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span>  <span class="o">-&gt;</span> <span class="n">next</span> <span class="n">ghostctl</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">.</span><span class="n">h</span><span class="p">.</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">012156</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">ipi_entry</span><span class="o">:</span> <span class="n">IRQ</span> <span class="n">work</span> <span class="n">interrupts</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">.</span><span class="n">h</span><span class="p">.</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">012157</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">ipi_exit</span><span class="o">:</span> <span class="n">IRQ</span> <span class="n">work</span> <span class="n">interrupts</span>
        <span class="n">ghostctl</span><span class="o">-</span><span class="mi">3518</span>    <span class="p">[</span><span class="mo">001</span><span class="p">]</span> <span class="n">d</span><span class="p">...</span>    <span class="mi">88</span><span class="p">.</span><span class="mo">0121</span><span class="mi">85</span><span class="o">:</span> <span class="n">bpf_trace_printk</span><span class="o">:</span> <span class="n">ipi_raise</span><span class="o">:</span> <span class="n">Function</span> <span class="n">call</span> <span class="n">interrupt</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="rouge-code"><pre>	<span class="k">if</span> <span class="p">(</span><span class="n">likely</span><span class="p">(</span><span class="n">prev</span><span class="o">-&gt;</span><span class="n">sched_class</span> <span class="o">&lt;=</span> <span class="o">&amp;</span><span class="n">fair_sched_class</span> <span class="o">&amp;&amp;</span>
		   <span class="n">rq</span><span class="o">-&gt;</span><span class="n">nr_running</span> <span class="o">==</span> <span class="n">rq</span><span class="o">-&gt;</span><span class="n">cfs</span><span class="p">.</span><span class="n">h_nr_running</span><span class="p">))</span> <span class="p">{</span>

<span class="c1">//prev-&gt;sched_class == idle_sched_class &lt;= fair_sched_class 满足条件</span>
<span class="c1">//rq-&gt;nr_running == rq-&gt;cfs.h_nr_running == 0 满足条件</span>
  
		<span class="n">p</span> <span class="o">=</span> <span class="n">pick_next_task_fair</span><span class="p">(</span><span class="n">rq</span><span class="p">,</span> <span class="n">prev</span><span class="p">,</span> <span class="n">rf</span><span class="p">);</span>  <span class="c1">//这里返回NULL</span>
		<span class="k">if</span> <span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="n">RETRY_TASK</span><span class="p">))</span>
			<span class="k">goto</span> <span class="n">restart</span><span class="p">;</span>

		<span class="cm">/* Assumes fair_sched_class-&gt;next == idle_sched_class */</span>
		<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span>
			<span class="n">put_prev_task</span><span class="p">(</span><span class="n">rq</span><span class="p">,</span> <span class="n">prev</span><span class="p">);</span>
			<span class="n">p</span> <span class="o">=</span> <span class="n">pick_next_task_idle</span><span class="p">(</span><span class="n">rq</span><span class="p">);</span> <span class="c1">//这里重新又pick idle，重新进去idle</span>
		<span class="p">}</span>

</pre></td></tr></tbody></table></code></div></div>

<p>搞清楚latched_task与ghost_rq-&gt;task_list的关系？</p>

<p>2、内核没有打开强制功能，内核中断不会触发主动调度</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="rouge-code"><pre>	<span class="p">.</span><span class="n">align</span>	<span class="mi">6</span>
<span class="n">SYM_CODE_START_LOCAL_NOALIGN</span><span class="p">(</span><span class="n">el1_irq</span><span class="p">)</span>
	<span class="n">kernel_entry</span> <span class="mi">1</span>
	<span class="n">gic_prio_irq_setup</span> <span class="n">pmr</span><span class="o">=</span><span class="n">x20</span><span class="p">,</span> <span class="n">tmp</span><span class="o">=</span><span class="n">x1</span>
	<span class="n">enable_da_f</span>

	<span class="n">mov</span>	<span class="n">x0</span><span class="p">,</span> <span class="n">sp</span>
	<span class="n">bl</span>	<span class="n">enter_el1_irq_or_nmi</span>

	<span class="n">irq_handler</span>

<span class="cp">#ifdef CONFIG_PREEMPTION  //内核抢占功能使能
</span>	<span class="n">ldr</span>	<span class="n">x24</span><span class="p">,</span> <span class="p">[</span><span class="n">tsk</span><span class="p">,</span> <span class="err">#</span><span class="n">TSK_TI_PREEMPT</span><span class="p">]</span>	<span class="c1">// get preempt count</span>
<span class="n">alternative_if</span> <span class="n">ARM64_HAS_IRQ_PRIO_MASKING</span>
	<span class="cm">/*
	 * DA_F were cleared at start of handling. If anything is set in DAIF,
	 * we come back from an NMI, so skip preemption
	 */</span>
	<span class="n">mrs</span>	<span class="n">x0</span><span class="p">,</span> <span class="n">daif</span>
	<span class="n">orr</span>	<span class="n">x24</span><span class="p">,</span> <span class="n">x24</span><span class="p">,</span> <span class="n">x0</span>
<span class="n">alternative_else_nop_endif</span>
	<span class="n">cbnz</span>	<span class="n">x24</span><span class="p">,</span> <span class="mi">1</span><span class="n">f</span>				<span class="c1">// preempt count != 0 || NMI return path</span>
	<span class="n">bl</span>	<span class="n">arm64_preempt_schedule_irq</span>	<span class="c1">// irq en/disable is done inside</span>
<span class="mi">1</span><span class="o">:</span>
<span class="cp">#endif
</span>
	<span class="n">mov</span>	<span class="n">x0</span><span class="p">,</span> <span class="n">sp</span>
	<span class="n">bl</span>	<span class="n">exit_el1_irq_or_nmi</span>

	<span class="n">kernel_exit</span> <span class="mi">1</span>
<span class="n">SYM_CODE_END</span><span class="p">(</span><span class="n">el1_irq</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></div></div>

<p>ghos怎么做到主动抢占的呢？</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//www.coolcou.com/linux-kernel/linux-kernel-references/linux-kernel-scheduling-process.html Linux内核调度流程-抢占的发生</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre>		<span class="n">ghost_task_new</span><span class="p">(</span><span class="n">rq</span><span class="p">,</span> <span class="n">prev</span><span class="p">);</span> <span class="c1">//给用户态发送task_new消息</span>
		<span class="n">ghost_wake_agent_of</span><span class="p">(</span><span class="n">prev</span><span class="p">);</span> <span class="c1">//prev</span>
</pre></td></tr></tbody></table></code></div></div>

<p>目前信息：ghost消息流程的后面如果需要主动抢占的，会调用wakeup agent，如果是非central的cpu，执行yeild_task-&gt;ghost_run-&gt;schedule()触发调度。</p>

<p>抢占点设计：复用内核抢占，打开内核抢占功能？？？</p>

<h4 id="2-ghost-userspace"><span class="me-2">2&gt; ghost-userspace</span><a href="#2-ghost-userspace" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>
<p>分两部分： 用户态代码 + ebpf代码</p>

<h5 id="1-ebpf机制"><span class="me-2">1&gt; ebpf机制</span><a href="#1-ebpf机制" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<h5 id="简介"><span class="me-2">简介</span><a href="#简介" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<p>​	BPF 是 <strong>Linux 内核中</strong>一个非常灵活与高效的<strong>类虚拟机</strong>（virtual machine-like）组件， 能够在许多内核 hook 点<strong>安全地</strong>执行字节码（bytecode ）。很多 <strong>内核子系统</strong>都已经使用了 BPF，例如常见的<strong>网络</strong>（networking）、<strong>跟踪</strong>（ tracing）与<strong>安全</strong>（security ，例如沙盒）。reference:https://arthurchiao.art/blog/cilium-bpf-xdp-reference-guide-zh/</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//pwl999.github.io/2018/09/28/bpf_kernel/ //从内核代码层面分析 bpf load verify run...</span>
</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/bpf-hooks.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/bpf-hooks.png" alt="image-20211202101430786" class="lazyload" data-proofer-ignore></a></p>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/bpf-map.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/bpf-map.png" alt="image-20211202102014012" class="lazyload" data-proofer-ignore></a></p>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/bpf-helper.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/bpf-helper.png" alt="image-20211202102205429" class="lazyload" data-proofer-ignore></a></p>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/bpf限制.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/bpf限制.png" alt="image-20211202102413705" class="lazyload" data-proofer-ignore></a></p>

<p>reference:https://zhuanlan.zhihu.com/p/373090595</p>

<h5 id="bpf-map"><span class="me-2">bpf map</span><a href="#bpf-map" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="rouge-code"><pre><span class="c1">//map</span>
<span class="k">struct</span> <span class="p">{</span>
        <span class="n">__uint</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="n">BPF_MAP_TYPE_HASH</span><span class="p">);</span>
        <span class="n">__uint</span><span class="p">(</span><span class="n">max_entries</span><span class="p">,</span> <span class="n">MAX_PIDS</span><span class="p">);</span>
        <span class="n">__type</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">u32</span><span class="p">);</span>
        <span class="n">__type</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="k">struct</span> <span class="n">task_stat</span><span class="p">);</span>
<span class="p">}</span> <span class="n">task_stats</span> <span class="nf">SEC</span><span class="p">(</span><span class="s">".maps"</span><span class="p">);</span>

<span class="k">struct</span> <span class="p">{</span>
        <span class="n">__uint</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="n">BPF_MAP_TYPE_PERCPU_ARRAY</span><span class="p">);</span>
        <span class="n">__uint</span><span class="p">(</span><span class="n">max_entries</span><span class="p">,</span> <span class="n">NR_HISTS</span><span class="p">);</span>
        <span class="n">__type</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">u32</span><span class="p">);</span>
        <span class="n">__type</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="k">struct</span> <span class="n">hist</span><span class="p">);</span>
<span class="p">}</span> <span class="n">hists</span> <span class="nf">SEC</span><span class="p">(</span><span class="s">".maps"</span><span class="p">);</span>
</pre></td></tr></tbody></table></code></div></div>

<h5 id="bpf-sec--bpf-hook"><span class="me-2">bpf SEC – bpf hook</span><a href="#bpf-sec--bpf-hook" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="c1">//SEC</span>
<span class="n">SEC</span><span class="p">(</span><span class="s">"tp_btf/sched_wakeup"</span><span class="p">)</span> 
<span class="kt">int</span> <span class="nf">BPF_PROG</span><span class="p">(</span><span class="n">sched_wakeup</span><span class="p">,</span> <span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">p</span><span class="p">)</span>
<span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">task_has_ghost_policy</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
                <span class="n">task_runnable</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
        <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
</pre></td><td class="rouge-code"><pre><span class="err">引用：</span><span class="n">https</span><span class="o">:</span><span class="c1">//www.shuzhiduo.com/A/kvJ3q9g7dg/</span>

<span class="cp">#include</span> <span class="cpf">"vmlinux.h"</span><span class="c1">   /* all kernel types */</span><span class="cp">
#include</span> <span class="cpf">&lt;bpf/bpf_helpers.h&gt;</span><span class="c1">  /* most used helpers: SEC, __always_inline, etc */</span><span class="cp">
#include</span> <span class="cpf">&lt;bpf/bpf_core_read.h&gt;</span><span class="c1">  /* for BPF CO-RE helpers */</span><span class="cp">
</span><span class="err">内核空间的</span><span class="n">BPF</span><span class="err">代码如下</span><span class="p">(</span><span class="err">假设生成的</span><span class="p">.</span><span class="n">o</span><span class="err">文件名为</span><span class="n">runqslower</span><span class="p">.</span><span class="n">bpf</span><span class="p">.</span><span class="n">o</span><span class="p">)</span><span class="err">：</span>

<span class="c1">// SPDX-License-Identifier: GPL-2.0</span>
<span class="c1">// Copyright (c) 2019 Facebook</span>
<span class="cm">/* BPF程序包含的头文件，可以看到内容想相当简洁 */</span>
<span class="cp">#include</span> <span class="cpf">"vmlinux.h"</span><span class="cp">
#include</span> <span class="cpf">&lt;bpf/bpf_helpers.h&gt;</span><span class="cp">
#include</span> <span class="cpf">"runqslower.h"</span><span class="cp">
#define TASK_RUNNING 0
#define BPF_F_CURRENT_CPU 0xffffffffULL
</span><span class="cm">/* 在BPF代码侧，可以使用一个 const volatile 声明只读的全局变量，只读的全局变量，变量最后会存在于runqslower.bpf.o的.rodata只读段，用户侧可以在BPF程序加载前读取或修改该只读段的参数【1】 */</span>
<span class="k">const</span> <span class="k">volatile</span> <span class="n">__u64</span> <span class="n">min_us</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">const</span> <span class="k">volatile</span> <span class="n">pid_t</span> <span class="n">targ_pid</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="cm">/* 定义名为 start 的map，类型为 BPF_MAP_TYPE_HASH。容量为10240，key类型为u32，value类型为u64。可以在【1】中查看BPF程序解析出来的.maps段【2】 */</span>
<span class="k">struct</span> <span class="p">{</span>
	<span class="n">__uint</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="n">BPF_MAP_TYPE_HASH</span><span class="p">);</span>
	<span class="n">__uint</span><span class="p">(</span><span class="n">max_entries</span><span class="p">,</span> <span class="mi">10240</span><span class="p">);</span>
	<span class="n">__type</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">u32</span><span class="p">);</span>
	<span class="n">__type</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">u64</span><span class="p">);</span>
<span class="p">}</span> <span class="n">start</span> <span class="nf">SEC</span><span class="p">(</span><span class="s">".maps"</span><span class="p">);</span>
<span class="cm">/* 由于 PERF_EVENT_ARRAY, STACK_TRACE 和其他特殊的maps(DEVMAP, CPUMAP, etc) 尚不支持key/value类型的BTF类型，因此需要直接指定 key_size/value_size */</span>
<span class="k">struct</span> <span class="p">{</span>
	<span class="n">__uint</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="n">BPF_MAP_TYPE_PERF_EVENT_ARRAY</span><span class="p">);</span>
	<span class="n">__uint</span><span class="p">(</span><span class="n">key_size</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">u32</span><span class="p">));</span>
	<span class="n">__uint</span><span class="p">(</span><span class="n">value_size</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">u32</span><span class="p">));</span>
<span class="p">}</span> <span class="n">events</span> <span class="nf">SEC</span><span class="p">(</span><span class="s">".maps"</span><span class="p">);</span>
<span class="cm">/* record enqueue timestamp */</span>
<span class="cm">/* 自定义的辅助函数必须标记为 static __always_inline。该函数用于保存唤醒的任务事件，key为pid，value为唤醒的时间点 */</span>
<span class="n">__always_inline</span>
<span class="k">static</span> <span class="kt">int</span> <span class="nf">trace_enqueue</span><span class="p">(</span><span class="n">u32</span> <span class="n">tgid</span><span class="p">,</span> <span class="n">u32</span> <span class="n">pid</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">u64</span> <span class="n">ts</span><span class="p">;</span>
	<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">pid</span> <span class="o">||</span> <span class="p">(</span><span class="n">targ_pid</span> <span class="o">&amp;&amp;</span> <span class="n">targ_pid</span> <span class="o">!=</span> <span class="n">pid</span><span class="p">))</span>
		<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
	<span class="n">ts</span> <span class="o">=</span> <span class="n">bpf_ktime_get_ns</span><span class="p">();</span>
	<span class="n">bpf_map_update_elem</span><span class="p">(</span><span class="o">&amp;</span><span class="n">start</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pid</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ts</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="cm">/* 所有BPF程序提供的功能都需要通过 SEC() (来自 bpf_helpers.h )宏来自定义section名称【3】。可以在【1】中查看BPF程序解析出来的自定义函数 */</span>
<span class="cm">/* 唤醒一个任务，并保存当前时间 */</span>
<span class="n">SEC</span><span class="p">(</span><span class="s">"tp_btf/sched_wakeup"</span><span class="p">)</span>
<span class="kt">int</span> <span class="nf">handle__sched_wakeup</span><span class="p">(</span><span class="n">u64</span> <span class="o">*</span><span class="n">ctx</span><span class="p">)</span>
<span class="p">{</span>
	<span class="cm">/* TP_PROTO(struct task_struct *p) */</span>
	<span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">ctx</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
	<span class="k">return</span> <span class="n">trace_enqueue</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">tgid</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">pid</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* 唤醒一个新创建的任务，并保存当前时间。BPF的上下文为一个task_struct*结构体 */</span>
<span class="n">SEC</span><span class="p">(</span><span class="s">"tp_btf/sched_wakeup_new"</span><span class="p">)</span>
<span class="kt">int</span> <span class="nf">handle__sched_wakeup_new</span><span class="p">(</span><span class="n">u64</span> <span class="o">*</span><span class="n">ctx</span><span class="p">)</span>
<span class="p">{</span>
	<span class="cm">/* TP_PROTO(struct task_struct *p) */</span>
	<span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">ctx</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
	<span class="k">return</span> <span class="n">trace_enqueue</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">tgid</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">pid</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* 计算一个任务入run队列到出队列的时间 */</span>
<span class="n">SEC</span><span class="p">(</span><span class="s">"tp_btf/sched_switch"</span><span class="p">)</span>
<span class="kt">int</span> <span class="nf">handle__sched_switch</span><span class="p">(</span><span class="n">u64</span> <span class="o">*</span><span class="n">ctx</span><span class="p">)</span>
<span class="p">{</span>
	<span class="cm">/* TP_PROTO(bool preempt, struct task_struct *prev,
	 *	    struct task_struct *next)
	 */</span>
	<span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">prev</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="p">)</span><span class="n">ctx</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
	<span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="n">next</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">task_struct</span> <span class="o">*</span><span class="p">)</span><span class="n">ctx</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
	<span class="k">struct</span> <span class="n">event</span> <span class="n">event</span> <span class="o">=</span> <span class="p">{};</span>
	<span class="n">u64</span> <span class="o">*</span><span class="n">tsp</span><span class="p">,</span> <span class="n">delta_us</span><span class="p">;</span>
	<span class="kt">long</span> <span class="n">state</span><span class="p">;</span>
	<span class="n">u32</span> <span class="n">pid</span><span class="p">;</span>
	<span class="cm">/* ivcsw: treat like an enqueue event and store timestamp */</span>
    <span class="cm">/* 如果被切换的任务的状态仍然是TASK_RUNNING，说明其又重新进入run队列，更新入队列的时间 */</span>
	<span class="k">if</span> <span class="p">(</span><span class="n">prev</span><span class="o">-&gt;</span><span class="n">state</span> <span class="o">==</span> <span class="n">TASK_RUNNING</span><span class="p">)</span>
		<span class="n">trace_enqueue</span><span class="p">(</span><span class="n">prev</span><span class="o">-&gt;</span><span class="n">tgid</span><span class="p">,</span> <span class="n">prev</span><span class="o">-&gt;</span><span class="n">pid</span><span class="p">);</span>
    <span class="cm">/* 获取下一个任务的PID */</span>
	<span class="n">pid</span> <span class="o">=</span> <span class="n">next</span><span class="o">-&gt;</span><span class="n">pid</span><span class="p">;</span>
	<span class="cm">/* fetch timestamp and calculate delta */</span>
    <span class="cm">/* 如果该任务并没有被唤醒，则无法正常进行任务切换，返回0即可 */</span>
	<span class="n">tsp</span> <span class="o">=</span> <span class="n">bpf_map_lookup_elem</span><span class="p">(</span><span class="o">&amp;</span><span class="n">start</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pid</span><span class="p">);</span>
	<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">tsp</span><span class="p">)</span>
		<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>   <span class="cm">/* missed enqueue */</span>
    <span class="cm">/* 当前切换时间减去该任务的入队列时间，计算进入run队列到真正调度的毫秒级时间 */</span>
	<span class="n">delta_us</span> <span class="o">=</span> <span class="p">(</span><span class="n">bpf_ktime_get_ns</span><span class="p">()</span> <span class="o">-</span> <span class="o">*</span><span class="n">tsp</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">;</span>
	<span class="k">if</span> <span class="p">(</span><span class="n">min_us</span> <span class="o">&amp;&amp;</span> <span class="n">delta_us</span> <span class="o">&lt;=</span> <span class="n">min_us</span><span class="p">)</span>
		<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
    <span class="cm">/* 更新events section，以便用户侧读取 */</span>
	<span class="n">event</span><span class="p">.</span><span class="n">pid</span> <span class="o">=</span> <span class="n">pid</span><span class="p">;</span>
	<span class="n">event</span><span class="p">.</span><span class="n">delta_us</span> <span class="o">=</span> <span class="n">delta_us</span><span class="p">;</span>
	<span class="n">bpf_get_current_comm</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">.</span><span class="n">task</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">event</span><span class="p">.</span><span class="n">task</span><span class="p">));</span>
	<span class="cm">/* output */</span>
	<span class="n">bpf_perf_event_output</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">events</span><span class="p">,</span> <span class="n">BPF_F_CURRENT_CPU</span><span class="p">,</span>
			      <span class="o">&amp;</span><span class="n">event</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">event</span><span class="p">));</span>
    <span class="cm">/* 该任务已经出队列，删除map */</span>
	<span class="n">bpf_map_delete_elem</span><span class="p">(</span><span class="o">&amp;</span><span class="n">start</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pid</span><span class="p">);</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">char</span> <span class="n">LICENSE</span><span class="p">[]</span> <span class="n">SEC</span><span class="p">(</span><span class="s">"license"</span><span class="p">)</span> <span class="o">=</span> <span class="s">"GPL"</span><span class="p">;</span>
</pre></td></tr></tbody></table></code></div></div>

<h5 id="bfp辅助函数"><span class="me-2">bfp辅助函数</span><a href="#bfp辅助函数" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<p>​	辅助函数是一组内核定义的函数集，<strong>使 eBPF 程序能从内核读取数据， 或者向内核写入数据</strong>（retrieve/push data from/to the kernel）</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="err">调用约定</span>
<span class="err">​</span>	<span class="err">辅助函数的调用约定（</span><span class="n">calling</span> <span class="n">convention</span><span class="err">）也是固定的：</span>
<span class="o">-</span> <span class="n">R0</span><span class="err">：存放程序返回值</span>
<span class="o">-</span> <span class="n">R1</span> <span class="o">~</span> <span class="n">R5</span><span class="err">：存放函数参数（</span><span class="n">function</span> <span class="n">arguments</span><span class="err">）</span>
<span class="o">-</span> <span class="n">R6</span> <span class="o">~</span> <span class="n">R9</span><span class="err">：</span><span class="o">**</span><span class="err">被调用方</span><span class="o">**</span><span class="err">（</span><span class="n">callee</span><span class="err">）负责保存的寄存器</span>
<span class="o">-</span> <span class="n">R10</span><span class="err">：栈空间</span> <span class="n">load</span><span class="o">/</span><span class="n">store</span> <span class="err">操作用的只读</span> <span class="n">frame</span> <span class="n">pointer</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">BPF_PROG_TYPE</span>
<span class="n">BPF_CALL</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">//介绍BPF_CALL </span>
<span class="n">https</span><span class="o">:</span><span class="c1">//arthurchiao.art/blog/on-getting-tc-classifier-fully-programmable-zh/</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="err">内核将辅助函数抽象成</span> <span class="n">BPF_CALL_0</span><span class="p">()</span> <span class="err">到</span> <span class="n">BPF_CALL_5</span><span class="p">()</span> <span class="err">几个宏，形式和相应类型的系</span> <span class="err">统调用类似。</span>
<span class="err">当前可用的</span> <span class="n">BPF</span> <span class="err">辅助函数已经有几十个，并且数量还在不断增加，例如，写作本文时，</span><span class="n">tc</span> <span class="n">BPF</span> <span class="err">程序可以使用</span><span class="mi">38</span> <span class="err">种不同的</span> <span class="n">BPF</span> <span class="err">辅助函数。对于一个给定的</span> <span class="n">BPF</span> <span class="err">程序类型，内核的</span> <span class="k">struct</span> <span class="n">bpf_verifier_ops</span> <span class="err">包含了</span> <span class="n">get_func_proto</span> <span class="err">回调函数，这个函数提供了从某个</span> <span class="err">特定的</span><span class="k">enum</span> <span class="n">bpf_func_id</span> <span class="err">到一个可用的辅助函数的映射</span><span class="p">.</span>
    
 <span class="n">reference</span><span class="o">:</span><span class="n">https</span><span class="o">:</span><span class="c1">//arthurchiao.art/blog/cilium-bpf-xdp-reference-guide-zh/#12-%E8%BE%85%E5%8A%A9%E5%87%BD%E6%95%B0</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">//get_func_proto介绍</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//blogs.oracle.com/linux/post/bpf-in-depth-bpf-helper-functions</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="c1">//译文附录：一些相关的 BPF 内核实现</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//arthurchiao.art/blog/lifetime-of-bpf-objects-zh/</span>
<span class="c1">//map</span>
<span class="n">bpf_create_map</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">bpf_create_map_xattr</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">sys_bpf</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">SYSCALL_DEFINE3</span><span class="p">(</span><span class="n">bpf</span><span class="p">,</span> <span class="p">...)</span><span class="err">：系统调用</span> <span class="n">SYSCALL_DEFINE3</span><span class="p">(</span><span class="n">bpf</span><span class="p">,</span> <span class="p">...)</span> <span class="o">-&gt;</span> <span class="k">case</span> <span class="n">BPF_MAP_CREATE</span> <span class="o">-&gt;</span> <span class="n">map_create</span><span class="p">()</span><span class="err">：创建</span> <span class="n">map</span>
<span class="c1">//load bpf program</span>
<span class="n">bpf</span><span class="p">(</span><span class="n">BPF_PROG_LOAD</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">sys_bpf</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">SYSCALL_DEFINE3</span><span class="p">(</span><span class="n">bpf</span><span class="p">,</span> <span class="p">...)</span> <span class="n">SYSCALL_DEFINE3</span><span class="p">(</span><span class="n">bpf</span><span class="p">,</span> <span class="p">...)</span> <span class="o">-&gt;</span> <span class="k">case</span> <span class="n">BPF_PROG_LOAD</span> <span class="o">-&gt;</span> <span class="n">bpf_prog_load</span><span class="p">()</span><span class="err">：加载逻辑</span>
<span class="n">bpf_prog_load</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">bpf_check</span><span class="p">()</span><span class="err">：执行内核校验</span> <span class="n">bpf_check</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">replace_map_fd_with_map_ptr</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">bpf_map_inc</span><span class="p">()</span><span class="err">：更新</span> <span class="n">map</span> <span class="n">refcnt</span>
</pre></td></tr></tbody></table></code></div></div>

<h5 id="2-用户态代码"><span class="me-2">2 用户态代码</span><a href="#2-用户态代码" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="rouge-code"><pre><span class="c1">//创建ghost class的进程</span>
<span class="n">GhostThread</span><span class="o">::</span><span class="n">GhostThread</span><span class="p">(</span><span class="n">KernelScheduler</span> <span class="n">ksched</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">()</span><span class="o">&gt;</span> <span class="n">work</span><span class="p">)</span>
    <span class="o">:</span> <span class="n">ksched_</span><span class="p">(</span><span class="n">ksched</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">GhostThread</span><span class="o">::</span><span class="n">SetGlobalEnclaveCtlFdOnce</span><span class="p">();</span>
  
  <span class="n">thread_</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="kr">thread</span><span class="p">([</span><span class="n">this</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">work</span><span class="p">)]</span> <span class="p">{</span>  <span class="c1">//进程创建</span>
    <span class="n">tid_</span> <span class="o">=</span> <span class="n">GetTID</span><span class="p">();</span>
    <span class="n">gtid_</span> <span class="o">=</span> <span class="n">Gtid</span><span class="o">::</span><span class="n">Current</span><span class="p">();</span>
  
    <span class="c1">// TODO: Consider moving after SchedEnterGhost.</span>
    <span class="n">started_</span><span class="p">.</span><span class="n">Notify</span><span class="p">();</span>
   
    <span class="k">if</span> <span class="p">(</span><span class="n">ksched_</span> <span class="o">==</span> <span class="n">KernelScheduler</span><span class="o">::</span><span class="n">kGhost</span><span class="p">)</span> <span class="p">{</span> 
      <span class="k">const</span> <span class="kt">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">SchedTaskEnterGhost</span><span class="p">(</span><span class="cm">/*pid=*/</span><span class="mi">0</span><span class="p">);</span>  <span class="c1">//如果是kGhost的话，设置为ghost_class调度类。</span>
      <span class="n">CHECK_EQ</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">w</span><span class="p">)();</span>
  <span class="p">});</span>
  <span class="n">started_</span><span class="p">.</span><span class="n">WaitForNotification</span><span class="p">();</span>  
<span class="p">}</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">//agent_shinjuku</span>
<span class="n">main</span>
    <span class="o">-&gt;</span><span class="n">ghost</span><span class="o">::</span><span class="n">AgentProcess</span><span class="o">&lt;</span><span class="n">ghost</span><span class="o">::</span><span class="n">FullShinjukuAgent</span><span class="o">&lt;</span><span class="n">ghost</span><span class="o">::</span><span class="n">LocalEnclave</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">ghost</span><span class="o">::</span><span class="n">ShinjukuConfig</span><span class="o">&gt;</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>

</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">__NR_memfd_create</span>
    
</pre></td></tr></tbody></table></code></div></div>

<h4 id="3-experiments"><span class="me-2">3&gt; experiments</span><a href="#3-experiments" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">//antagonist</span>
<span class="p">.</span><span class="o">/</span><span class="n">agent</span><span class="o">-</span><span class="n">sol</span> <span class="err">或者</span><span class="n">agent</span><span class="o">-</span><span class="n">shinjuku</span>
<span class="p">.</span><span class="o">/</span><span class="n">antagonist</span> <span class="o">--</span><span class="n">scheduler</span><span class="o">=</span><span class="n">ghost</span> <span class="o">--</span><span class="n">cpus</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="rouge-code"><pre><span class="c1">//rocksdb</span>
<span class="p">.</span><span class="o">/</span><span class="n">rocksdb</span> <span class="o">--</span><span class="n">scheduler</span><span class="o">=</span><span class="n">ghost</span> <span class="o">--</span><span class="n">rocksdb_db_path</span><span class="o">=/</span><span class="n">home</span><span class="o">/</span><span class="n">rocksdb</span><span class="o">/</span>
  <span class="c1">///cfs</span>
<span class="p">.</span><span class="o">/</span><span class="n">rocksdb</span> <span class="o">--</span><span class="n">scheduler</span><span class="o">=</span><span class="n">cfs</span> <span class="o">--</span><span class="n">rocksdb_db_path</span><span class="o">=/</span><span class="n">home</span><span class="o">/</span><span class="n">rocksdb</span><span class="o">/</span> <span class="o">--</span><span class="n">throughput</span><span class="o">=</span><span class="mi">200000</span> <span class="o">--</span><span class="n">num_workers</span><span class="o">=</span><span class="mi">20</span> <span class="o">--</span><span class="n">worker_cpus</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">11</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">13</span><span class="p">,</span><span class="mi">14</span><span class="p">,</span><span class="mi">15</span><span class="p">,</span><span class="mi">16</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">18</span><span class="p">,</span><span class="mi">19</span><span class="p">,</span><span class="mi">21</span>  <span class="o">--</span><span class="n">batch</span><span class="o">=</span><span class="mi">1</span>
  <span class="n">All</span><span class="o">:</span>
<span class="n">Stage</span>                        <span class="n">Total</span> <span class="n">Requests</span>     <span class="n">Throughput</span> <span class="p">(</span><span class="n">req</span><span class="o">/</span><span class="n">s</span><span class="p">)</span>     <span class="n">Min</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>     <span class="mi">50</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>     <span class="mi">99</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>     <span class="mi">99</span><span class="p">.</span><span class="mi">5</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>   <span class="mi">99</span><span class="p">.</span><span class="mi">9</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>   <span class="n">Max</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>    
<span class="o">----------------------------------------------------------------------------------------------------------------------------------------------------</span>
<span class="n">Ingress</span> <span class="n">Queue</span> <span class="n">Time</span>           <span class="mi">23528589</span>           <span class="mi">199938</span>                 <span class="mi">0</span>            <span class="mi">7559</span>         <span class="mi">2293590</span>      <span class="mi">2537468</span>      <span class="mi">2729921</span>      <span class="mi">2786260</span>     
<span class="n">Repeatable</span> <span class="n">Handle</span> <span class="n">Time</span>       <span class="mi">23528589</span>           <span class="mi">199938</span>                 <span class="mi">0</span>            <span class="mi">854</span>          <span class="mi">177527</span>       <span class="mi">206358</span>       <span class="mi">247805</span>       <span class="mi">254317</span>      
<span class="n">Worker</span> <span class="n">Queue</span> <span class="n">Time</span>            <span class="mi">23528589</span>           <span class="mi">199938</span>                 <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">1</span>            <span class="mi">4</span>            <span class="mi">25983</span>       
<span class="n">Worker</span> <span class="n">Handle</span> <span class="n">Time</span>           <span class="mi">23528589</span>           <span class="mi">199938</span>                 <span class="mi">10</span>           <span class="mi">10</span>           <span class="mi">13</span>           <span class="mi">14</span>           <span class="mi">15</span>           <span class="mi">13019</span>       
<span class="n">Total</span>                        <span class="mi">23528589</span>           <span class="mi">199938</span>                 <span class="mi">11</span>           <span class="mi">9601</span>         <span class="mi">2305599</span>      <span class="mi">2549779</span>      <span class="mi">2739167</span>      <span class="mi">2800522</span> 
    <span class="c1">//ghost</span>
<span class="p">.</span><span class="o">/</span><span class="n">rocksdb</span> <span class="o">--</span><span class="n">scheduler</span><span class="o">=</span><span class="n">ghost</span> <span class="o">--</span><span class="n">rocksdb_db_path</span><span class="o">=/</span><span class="n">home</span><span class="o">/</span><span class="n">rocksdb</span><span class="o">/</span> <span class="o">--</span><span class="n">throughput</span><span class="o">=</span><span class="mi">200000</span> <span class="o">--</span><span class="n">num_workers</span><span class="o">=</span><span class="mi">20</span>  <span class="o">--</span><span class="n">batch</span><span class="o">=</span><span class="mi">1</span>
    
<span class="n">Stage</span>                        <span class="n">Total</span> <span class="n">Requests</span>     <span class="n">Throughput</span> <span class="p">(</span><span class="n">req</span><span class="o">/</span><span class="n">s</span><span class="p">)</span>     <span class="n">Min</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>     <span class="mi">50</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>     <span class="mi">99</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>     <span class="mi">99</span><span class="p">.</span><span class="mi">5</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>   <span class="mi">99</span><span class="p">.</span><span class="mi">9</span><span class="o">%</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>   <span class="n">Max</span> <span class="p">(</span><span class="n">us</span><span class="p">)</span>    
<span class="o">----------------------------------------------------------------------------------------------------------------------------------------------------</span>
<span class="n">Ingress</span> <span class="n">Queue</span> <span class="n">Time</span><span class="err">（入队排队耗时）</span>           <span class="mi">8820243</span>            <span class="mi">200041</span>                 <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">32501</span>        <span class="mi">47881</span>        <span class="mi">71177</span>        <span class="mi">80389</span>       
<span class="n">Repeatable</span> <span class="n">Handle</span> <span class="n">Time</span>       <span class="mi">8820243</span>            <span class="mi">200041</span>                 <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">86</span>          
<span class="n">Worker</span> <span class="n">Queue</span> <span class="n">Time</span> <span class="err">（查询耗时）</span>           <span class="mi">8820243</span>            <span class="mi">200041</span>                 <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">0</span>            <span class="mi">4</span>            <span class="mi">694068</span>      
<span class="n">Worker</span> <span class="n">Handle</span> <span class="n">Time</span>           <span class="mi">8820243</span>            <span class="mi">200041</span>                 <span class="mi">10</span>           <span class="mi">10</span>           <span class="mi">13</span>           <span class="mi">13</span>           <span class="mi">15</span>           <span class="mi">803878</span>      
<span class="n">Total</span>                        <span class="mi">8820243</span>            <span class="mi">200041</span>                 <span class="mi">10</span>           <span class="mi">11</span>           <span class="mi">34104</span>        <span class="mi">50669</span>        <span class="mi">76510</span>        <span class="mi">803880</span> 
<span class="err">在</span><span class="mi">200000</span><span class="n">req</span><span class="o">/</span><span class="n">s</span><span class="err">情况下，</span> <span class="mi">1</span> <span class="n">batch</span><span class="err">任务，</span> <span class="mi">20</span><span class="err">个</span><span class="n">work</span><span class="err">来处理</span><span class="n">req</span><span class="err">时，</span><span class="n">ghost</span><span class="err">调度的</span><span class="mi">99</span><span class="o">%</span><span class="err">的时延比</span><span class="n">cfs</span><span class="err">号很多</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c++ highlighter-rouge"><div class="code-header">
        <span data-label-text="C++"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="rouge-code"><pre><span class="n">Thread</span> <span class="mi">1</span> <span class="s">"rocksdb"</span> <span class="n">received</span> <span class="n">signal</span> <span class="n">SIGABRT</span><span class="p">,</span> <span class="n">Aborted</span><span class="p">.</span>
<span class="mh">0x00007ffff7b5bdd3</span> <span class="n">in</span> <span class="n">pthread_kill</span> <span class="p">()</span> <span class="n">from</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib64</span><span class="o">/</span><span class="n">libc</span><span class="p">.</span><span class="n">so</span><span class="mf">.6</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span> <span class="n">bt</span>
<span class="cp">#0  0x00007ffff7b5bdd3 in pthread_kill () from /usr/lib64/libc.so.6
#1  0x00007ffff7b0ffc6 in raise () from /usr/lib64/libc.so.6
#2  0x00007ffff7afb457 in abort () from /usr/lib64/libc.so.6
#3  0x00007ffff7e93c44 in ?? () from /usr/lib64/libstdc++.so.6
#4  0x00007ffff7f12212 in std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::back() () from /usr/lib64/libstdc++.so.6
#5  0x000000000053dc4f in rocksdb::SanitizeOptions(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, rocksdb::DBOptions const&amp;) ()
#6  0x000000000083ce2b in rocksdb::DBImpl::DBImpl(rocksdb::DBOptions const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, bool, bool) ()
#7  0x0000000000546c68 in rocksdb::DBImpl::Open(rocksdb::DBOptions const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;rocksdb::ColumnFamilyDescriptor, std::allocator&lt;rocksdb::ColumnFamilyDescriptor&gt; &gt; const&amp;, std::vector&lt;rocksdb::ColumnFamilyHandle*, std::allocator&lt;rocksdb::ColumnFamilyHandle*&gt; &gt;*, rocksdb::DB**, bool, bool) ()
#8  0x00000000005463a2 in rocksdb::DB::Open(rocksdb::DBOptions const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::vector&lt;rocksdb::ColumnFamilyDescriptor, std::allocator&lt;rocksdb::ColumnFamilyDescriptor&gt; &gt; const&amp;, std::vector&lt;rocksdb::ColumnFamilyHandle*, std::allocator&lt;rocksdb::ColumnFamilyHandle*&gt; &gt;*, rocksdb::DB**) ()
#9  0x0000000000546158 in rocksdb::DB::Open(rocksdb::Options const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, rocksdb::DB**)
</span>    <span class="p">()</span>
<span class="cp">#10 0x0000000000421e41 in ghost_test::Database::OpenDatabase(std::filesystem::__cxx11::path const&amp;) ()
#11 0x0000000000421f26 in ghost_test::Database::Database(std::filesystem::__cxx11::path const&amp;) ()
#12 0x000000000043fa9b in ghost_test::Orchestrator::Orchestrator(ghost_test::Orchestrator::Options, unsigned long) ()
#13 0x0000000000426789 in ghost_test::GhostOrchestrator::GhostOrchestrator(ghost_test::Orchestrator::Options) ()
#14 0x000000000043a586 in std::_MakeUniq&lt;ghost_test::GhostOrchestrator&gt;::__single_object std::make_unique&lt;ghost_test::GhostOrchestrator, ghost_test::Orchestrator::Options&amp;&gt;(ghost_test::Orchestrator::Options&amp;) ()
</span></pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="rouge-code"><pre><span class="n">main</span>
    <span class="o">-&gt;</span><span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">ghost_test</span><span class="o">::</span><span class="n">GhostOrchestrator</span><span class="o">&gt;</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>  <span class="c1">//main.cc</span>
        <span class="o">-&gt;</span><span class="n">GhostOrchestrator</span><span class="o">::</span><span class="n">GhostOrchestrator</span><span class="p">(</span><span class="n">Orchestrator</span><span class="o">::</span><span class="n">Options</span> <span class="n">opts</span><span class="p">)</span> <span class="c1">//rocksdb/ghost_orchestrator.cc</span>
            <span class="o">-&gt;</span> <span class="n">Orchestrator</span><span class="o">::</span><span class="n">Orchestrator</span><span class="p">(</span><span class="n">Options</span> <span class="n">options</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">total_threads</span><span class="p">)</span> <span class="c1">//experiments/rocksdb/orchestrator.cc</span>
            <span class="c1">//database、thread_pool构造函数 创建了database和thread_pool.</span>
            <span class="p">{</span>
                <span class="o">-&gt;</span><span class="n">InitThreadPool</span><span class="p">()</span>
                <span class="c1">//kernel_schedulers(cfs, ghost, ghost,..) thread_work(GhostOrchestrator::LoadGenerator, GhostOrchestrator::Worker, GhostOrchestrator::Worker, ..) </span>
                 <span class="c1">//thread_pool().Init(kernel_schedulers, thread_work); </span>
                <span class="o">-&gt;</span><span class="n">InitGhost</span><span class="p">()</span>
                  <span class="c1">//设置进程ghost参数</span>
            <span class="p">}</span>
       
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="rouge-code"><pre><span class="c1">// thread_pool.Init实现： 根据KernelScheduler[]类型设置线程sched_attr，并执行thread_work[]</span>
 <span class="mi">27</span> <span class="kt">void</span> <span class="n">ExperimentThreadPool</span><span class="o">::</span><span class="n">Init</span><span class="p">(</span>
 <span class="mi">28</span>     <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">ghost</span><span class="o">::</span><span class="n">GhostThread</span><span class="o">::</span><span class="n">KernelScheduler</span><span class="o">&gt;&amp;</span> <span class="n">ksched</span><span class="p">,</span>
 <span class="mi">29</span>     <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">(</span><span class="kt">uint32_t</span><span class="p">)</span><span class="o">&gt;&gt;&amp;</span> <span class="n">thread_work</span><span class="p">)</span> <span class="p">{</span>
 <span class="mi">30</span>   <span class="n">CHECK_EQ</span><span class="p">(</span><span class="n">ksched</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span> <span class="n">num_threads_</span><span class="p">);</span>
 <span class="mi">31</span>   <span class="n">CHECK_EQ</span><span class="p">(</span><span class="n">ksched</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span> <span class="n">thread_work</span><span class="p">.</span><span class="n">size</span><span class="p">());</span>
 <span class="mi">32</span> 
 <span class="mi">33</span>   <span class="n">threads_</span><span class="p">.</span><span class="n">reserve</span><span class="p">(</span><span class="n">num_threads_</span><span class="p">);</span>
 <span class="mi">34</span>   <span class="k">for</span> <span class="p">(</span><span class="kt">uint32_t</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">num_threads_</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
 <span class="mi">35</span>     <span class="n">threads_</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">ghost</span><span class="o">::</span><span class="n">GhostThread</span><span class="o">&gt;</span><span class="p">(</span>  <span class="c1">//根据ksched的type设置进程的sched_attr.</span>
 <span class="mi">36</span>         <span class="n">ksched</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
 <span class="mi">37</span>         <span class="n">std</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ExperimentThreadPool</span><span class="o">::</span><span class="n">ThreadMain</span><span class="p">,</span> <span class="n">this</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">thread_work</span><span class="p">[</span><span class="n">i</span><span class="p">])));</span> <span class="c1">//ThreadMain函数执行thread_work[i]</span>
 <span class="mi">38</span>   <span class="p">}</span>
	<span class="p">...</span>
 <span class="mi">45</span> <span class="kt">void</span> <span class="n">ExperimentThreadPool</span><span class="o">::</span><span class="n">ThreadMain</span><span class="p">(</span>
 <span class="mi">46</span>     <span class="kt">uint32_t</span> <span class="n">i</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">(</span><span class="kt">uint32_t</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">thread_work</span><span class="p">)</span> <span class="p">{</span>
 <span class="mi">47</span>   <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">ShouldExit</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="p">{</span>
 <span class="mi">48</span>     <span class="n">thread_work</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
 <span class="mi">49</span>   <span class="p">}</span>
 <span class="mi">50</span>   <span class="n">num_exited_</span><span class="p">.</span><span class="n">fetch_add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">memory_order_release</span><span class="p">);</span>
 <span class="mi">51</span> <span class="p">}</span>
</pre></td></tr></tbody></table></code></div></div>

<h4 id="3-ghost用户态工具的编译"><span class="me-2">3&gt; ghost用户态工具的编译</span><a href="#3-ghost用户态工具的编译" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<h5 id="1-bazel"><span class="me-2">1 bazel</span><a href="#1-bazel" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">//增加-g参数</span>
<span class="n">bazel</span> <span class="n">build</span> <span class="o">-</span><span class="n">c</span> <span class="n">opt</span> <span class="o">--</span><span class="n">copt</span><span class="o">=</span><span class="s">"-g"</span> <span class="o">--</span><span class="n">cxxopt</span><span class="o">=</span><span class="s">"-g"</span> <span class="o">--</span><span class="n">host_copt</span><span class="o">=</span><span class="s">"-g"</span> <span class="o">--</span><span class="n">host_cxxopt</span><span class="o">=</span><span class="s">"-g"</span> <span class="p">...</span>
<span class="c1">//~/.bazelrc</span>
<span class="n">build</span> <span class="o">--</span><span class="n">cxxopt</span><span class="o">=</span><span class="err">'</span><span class="o">-</span><span class="n">std</span><span class="o">=</span><span class="n">c</span><span class="o">++</span><span class="mi">17</span><span class="err">'</span> <span class="o">--</span><span class="n">cxxopt</span><span class="o">=</span><span class="err">'</span><span class="o">-</span><span class="n">g</span><span class="err">'</span>
</pre></td></tr></tbody></table></code></div></div>

<h6 id="简介-1">简介</h6>

<p><strong>Bazel</strong><strong>的目标之一是创建一个构建系统</strong>，在这个系统中，<strong>构建目标的输入和输出是完全指定的</strong>，因此构建系统可以<strong>精确地知道它们的输入和输出</strong>，这样可以<strong>更准确地分析和确定构建系统依赖图中过时的构建工件</strong>。<strong>使依赖图分析更加准确</strong>，通过<strong>避免重新执行不必要的构建目标</strong>，从而可能改善构建时间。<strong>通过避免构建目标可能依赖于过时的输入工件的错误，提高了构建的可靠性</strong>。</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//docs.bazel.build/versions/main/command-line-reference.html help文件</span>
</pre></td></tr></tbody></table></code></div></div>

<h6 id="依赖">依赖</h6>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">cmake</span> <span class="n">make</span> <span class="n">gcc</span> <span class="n">gcc</span><span class="o">-</span><span class="n">c</span><span class="o">++</span> <span class="n">elfutils</span><span class="o">-</span><span class="n">devel</span> <span class="n">numactl</span><span class="o">-</span><span class="n">devel</span> <span class="n">numactl</span><span class="o">-</span><span class="n">libs</span>  <span class="n">libbpf</span> <span class="n">libbpf</span><span class="o">-</span><span class="n">devel</span> <span class="n">bcc</span> <span class="n">bpftools</span> <span class="n">libcap</span><span class="o">-</span><span class="n">devel</span> <span class="n">llvm</span> <span class="n">llvm</span><span class="o">-</span><span class="n">devel</span> <span class="n">python3</span><span class="o">-</span><span class="n">pip</span>
</pre></td></tr></tbody></table></code></div></div>

<h6 id="编译">编译</h6>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="cp">##-g参数
</span><span class="n">bazel</span> <span class="n">build</span> <span class="o">-</span><span class="n">c</span> <span class="n">opt</span> <span class="o">--</span><span class="n">copt</span><span class="o">=</span><span class="s">"-g"</span> <span class="o">--</span><span class="n">cxxopt</span><span class="o">=</span><span class="s">"-g"</span> <span class="o">--</span><span class="n">host_copt</span><span class="o">=</span><span class="s">"-g"</span> <span class="o">--</span><span class="n">host_cxxopt</span><span class="o">=</span><span class="s">"-g"</span> <span class="p">...</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="c1">//http文件服务器搭建：</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//blog.51cto.com/soysauce93/1725318</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//www.cnblogs.com/zhuyeshen/p/11693362.html</span>
<span class="c1">//遇事不行就关闭防火墙：</span>
<span class="n">systemctl</span> <span class="n">stop</span> <span class="n">firewalld</span><span class="p">.</span><span class="n">service</span>
<span class="n">Forbidden</span><span class="err">：</span><span class="n">You</span> <span class="n">don</span><span class="err">'</span><span class="n">t</span> <span class="n">have</span> <span class="n">permission</span> <span class="n">to</span> <span class="n">access</span> <span class="n">this</span> <span class="n">resource</span><span class="p">.</span>
<span class="c1">//遇到说权限不足的，注意看一下目录权限</span>
<span class="c1">//修改文件web服务器</span>
<span class="n">sed</span> <span class="o">-</span><span class="n">i</span> <span class="s">"s/https:\/\/github.com\/bazelbuild\/rules_foreign_cc\/archive\//http:\/\/x.x.x.x\/bazel\//g"</span> <span class="n">WORKSPACE</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="rouge-code"><pre><span class="err">直接编译方法：</span>
<span class="mi">1</span><span class="err">、解决</span><span class="n">unable</span> <span class="n">to</span> <span class="n">find</span> <span class="n">valid</span> <span class="n">certification</span> <span class="n">path</span> <span class="n">to</span> <span class="n">requested</span> <span class="n">target</span>     
<span class="mi">1</span><span class="o">&gt;</span><span class="err">先按照这个</span><span class="o">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</span>
		<span class="c1">//指定位置和密码</span>
         <span class="n">alias</span> <span class="n">bazel</span><span class="o">=</span><span class="err">'</span><span class="n">bazel</span> <span class="o">--</span><span class="n">host_jvm_args</span><span class="o">=-</span><span class="n">Djavax</span><span class="p">.</span><span class="n">net</span><span class="p">.</span><span class="n">ssl</span><span class="p">.</span><span class="n">trustStore</span><span class="o">=/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">jvm</span><span class="o">/</span><span class="n">java</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="n">openjdk</span><span class="o">-</span><span class="mi">11</span><span class="p">.</span><span class="mi">0</span><span class="p">.</span><span class="mi">9</span><span class="p">.</span><span class="mi">11</span><span class="o">-</span><span class="mi">4</span><span class="p">.</span><span class="n">oe1</span><span class="p">.</span><span class="n">x86_64</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">security</span><span class="o">/</span><span class="n">cacerts</span> <span class="o">--</span><span class="n">host_jvm_args</span><span class="o">=-</span><span class="n">Djavax</span><span class="p">.</span><span class="n">net</span><span class="p">.</span><span class="n">ssl</span><span class="p">.</span><span class="n">trustStorePassword</span><span class="o">=</span><span class="n">changeit</span><span class="err">'</span>
         <span class="n">bazel</span> <span class="o">--</span><span class="n">host_jvm_args</span><span class="o">=-</span><span class="n">Djavax</span><span class="p">.</span><span class="n">net</span><span class="p">.</span><span class="n">ssl</span><span class="p">.</span><span class="n">trustStore</span><span class="o">=/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">jvm</span><span class="o">/</span><span class="n">java</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="n">openjdk</span><span class="o">-</span><span class="mi">11</span><span class="p">.</span><span class="mi">0</span><span class="p">.</span><span class="mi">9</span><span class="p">.</span><span class="mi">11</span><span class="o">-</span><span class="mi">4</span><span class="p">.</span><span class="n">oe1</span><span class="p">.</span><span class="n">x86_64</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">security</span><span class="o">/</span><span class="n">cacerts</span> <span class="o">--</span><span class="n">host_jvm_args</span><span class="o">=-</span><span class="n">Djavax</span><span class="p">.</span><span class="n">net</span><span class="p">.</span><span class="n">ssl</span><span class="p">.</span><span class="n">trustStorePassword</span><span class="o">=</span><span class="n">changeit</span> <span class="n">build</span> <span class="n">agent_shinjuku</span>    
 
<span class="mi">2</span><span class="o">&gt;</span> <span class="err">安装对应网站证书</span> <span class="o">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</span>
	<span class="c1">//导入证书：点击左上角的锁icon -&gt; 证书 -&gt; 详细信息 -&gt; 复制到文件 -&gt; 选择Base64编码的X.509格式，保存证书到本地目录</span>
	<span class="n">keytool</span> <span class="o">-</span><span class="n">import</span> <span class="o">-</span><span class="n">file</span> <span class="o">/</span><span class="n">root</span><span class="o">/</span><span class="n">caCert</span><span class="p">.</span><span class="n">cer</span> <span class="o">-</span><span class="n">keystore</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">jvm</span><span class="o">/</span><span class="n">java</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="n">openjdk</span><span class="o">-</span><span class="mi">11</span><span class="p">.</span><span class="mi">0</span><span class="p">.</span><span class="mi">9</span><span class="p">.</span><span class="mi">11</span><span class="o">-</span><span class="mi">4</span><span class="p">.</span><span class="n">oe1</span><span class="p">.</span><span class="n">x86_64</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">security</span><span class="o">/</span><span class="n">cacerts</span>  <span class="o">-</span><span class="n">trustcacerts</span> <span class="o">-</span><span class="n">alias</span> <span class="n">github_crt</span> <span class="o">-</span><span class="n">storepass</span> <span class="n">changeit</span>
        
  <span class="mi">2</span><span class="err">、为了快捷下载</span><span class="n">linux</span><span class="err">包，采用本地</span><span class="n">web</span><span class="err">文件服务器</span>
        <span class="n">systemctl</span> <span class="n">start</span> <span class="n">httpd</span>
        <span class="n">systemctl</span> <span class="n">stop</span> <span class="n">firewalld</span><span class="p">.</span><span class="n">service</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="rouge-code"><pre><span class="c1">//编译遇到pip CERTIFICATE_VERIFY_FAILED失败：</span>
<span class="n">SSL</span><span class="o">:</span> <span class="n">CERTIFICATE_VERIFY_FAILED</span><span class="p">]</span> <span class="n">certificate</span> <span class="n">verify</span> <span class="n">failed</span><span class="o">:</span> <span class="n">self</span> <span class="kt">signed</span> <span class="n">certificate</span> <span class="n">in</span> <span class="n">certificate</span> <span class="n">chain</span> <span class="p">(</span><span class="n">_ssl</span><span class="p">.</span><span class="n">c</span><span class="o">:</span><span class="mi">1123</span><span class="p">)</span>
<span class="err">解决：</span><span class="n">ssh</span><span class="o">-</span><span class="n">keygen</span>
<span class="c1">//etc/pip.conf</span>
<span class="p">[</span><span class="n">root</span><span class="err">@</span><span class="n">localhost</span> <span class="n">ghost</span><span class="o">-</span><span class="n">userspace</span><span class="p">]</span><span class="err">#</span> <span class="n">cat</span> <span class="o">~/</span><span class="p">.</span><span class="n">pip</span><span class="o">/</span><span class="n">pip</span><span class="p">.</span><span class="n">conf</span> 
<span class="p">[</span><span class="n">global</span><span class="p">]</span>
<span class="n">trusted</span><span class="o">-</span><span class="n">host</span> <span class="o">=</span> <span class="n">pypi</span><span class="p">.</span><span class="n">python</span><span class="p">.</span><span class="n">org</span>
               <span class="n">pypi</span><span class="p">.</span><span class="n">org</span>
               <span class="n">files</span><span class="p">.</span><span class="n">pythonhosted</span><span class="p">.</span><span class="n">org</span>
<span class="n">verify</span> <span class="o">=</span> <span class="nb">false</span>
<span class="c1">//etc/pip.conf</span>
<span class="p">[</span><span class="n">root</span><span class="err">@</span><span class="n">localhost</span> <span class="n">ghost</span><span class="o">-</span><span class="n">userspace</span><span class="p">]</span><span class="err">#</span> <span class="n">cat</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">pip</span><span class="p">.</span><span class="n">conf</span> 
<span class="p">[</span><span class="n">global</span><span class="p">]</span>
<span class="n">trusted</span><span class="o">-</span><span class="n">host</span> <span class="o">=</span> <span class="n">pypi</span><span class="p">.</span><span class="n">python</span><span class="p">.</span><span class="n">org</span>
               <span class="n">pypi</span><span class="p">.</span><span class="n">org</span>
               <span class="n">files</span><span class="p">.</span><span class="n">pythonhosted</span><span class="p">.</span><span class="n">org</span>
<span class="p">[</span><span class="n">http</span><span class="p">]</span>
<span class="n">sslVerify</span> <span class="o">=</span> <span class="nb">false</span>

<span class="p">[</span><span class="n">https</span><span class="p">]</span>
<span class="n">sslVerify</span> <span class="o">=</span> <span class="nb">false</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="rouge-code"><pre><span class="c1">//编译失败 'always_inline' 'uint8x16_t vaesmcq_u8(uint8x16_t)'</span>
<span class="n">In</span> <span class="n">file</span> <span class="n">included</span> <span class="n">from</span> <span class="n">external</span><span class="o">/</span><span class="n">com_google_absl</span><span class="o">/</span><span class="n">absl</span><span class="o">/</span><span class="n">random</span><span class="o">/</span><span class="n">internal</span><span class="o">/</span><span class="n">randen_hwaes</span><span class="p">.</span><span class="n">cc</span><span class="o">:</span><span class="mi">229</span><span class="o">:</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">gcc</span><span class="o">/</span><span class="n">aarch64</span><span class="o">-</span><span class="n">linux</span><span class="o">-</span><span class="n">gnu</span><span class="o">/</span><span class="mi">10</span><span class="p">.</span><span class="mi">3</span><span class="p">.</span><span class="mi">1</span><span class="o">/</span><span class="n">include</span><span class="o">/</span><span class="n">arm_neon</span><span class="p">.</span><span class="n">h</span><span class="o">:</span> <span class="n">In</span> <span class="n">function</span> <span class="err">'</span><span class="n">Vector128</span> <span class="p">{</span><span class="n">anonymous</span><span class="p">}</span><span class="o">::</span><span class="n">AesRound</span><span class="p">(</span><span class="k">const</span> <span class="n">Vector128</span><span class="o">&amp;</span><span class="p">,</span> <span class="k">const</span> <span class="n">Vector128</span><span class="o">&amp;</span><span class="p">)</span><span class="err">'</span><span class="o">:</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">gcc</span><span class="o">/</span><span class="n">aarch64</span><span class="o">-</span><span class="n">linux</span><span class="o">-</span><span class="n">gnu</span><span class="o">/</span><span class="mi">10</span><span class="p">.</span><span class="mi">3</span><span class="p">.</span><span class="mi">1</span><span class="o">/</span><span class="n">include</span><span class="o">/</span><span class="n">arm_neon</span><span class="p">.</span><span class="n">h</span><span class="o">:</span><span class="mi">12332</span><span class="o">:</span><span class="mi">1</span><span class="o">:</span> <span class="n">error</span><span class="o">:</span> <span class="n">inlining</span> <span class="n">failed</span> <span class="n">in</span> <span class="n">call</span> <span class="n">to</span> <span class="err">'</span><span class="n">always_inline</span><span class="sc">' '</span><span class="n">uint8x16_t</span> <span class="n">vaesmcq_u8</span><span class="p">(</span><span class="n">uint8x16_t</span><span class="p">)</span><span class="err">'</span><span class="o">:</span> <span class="n">target</span> <span class="n">specific</span> <span class="n">option</span> <span class="n">mismatch</span>
<span class="mi">12332</span> <span class="o">|</span> <span class="n">vaesmcq_u8</span> <span class="p">(</span><span class="n">uint8x16_t</span> <span class="n">data</span><span class="p">)</span>
<span class="c1">//解决： + cpu_aarch64</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//github.com/abseil/abseil-cpp/commit/2e94e5b6e152df9fa9c2fe8c1b96e1393973d32c</span>
    <span class="s">":cpu_x64_windows"</span><span class="o">:</span> <span class="n">ABSL_RANDOM_HWAES_MSVC_X64_FLAGS</span><span class="p">,</span>
    <span class="s">":cpu_k8"</span><span class="o">:</span> <span class="n">ABSL_RANDOM_HWAES_X64_FLAGS</span><span class="p">,</span>
    <span class="s">":cpu_ppc"</span><span class="o">:</span> <span class="p">[</span><span class="s">"-mcrypto"</span><span class="p">],</span>
   <span class="c1">//+ ":cpu_aarch64": ABSL_RANDOM_HWAES_ARM64_FLAGS,</span>

    <span class="cp"># Supported by default or unsupported.
</span>    <span class="s">"//conditions:default"</span><span class="o">:</span> <span class="p">[],</span>
	<span class="err">@@</span> <span class="o">-</span><span class="mi">70</span><span class="p">,</span><span class="mi">6</span> <span class="o">+</span><span class="mi">71</span><span class="p">,</span><span class="mi">7</span> <span class="err">@@</span> <span class="n">def</span> <span class="n">absl_random_randen_copts_init</span><span class="p">()</span><span class="o">:</span>
        <span class="s">"darwin"</span><span class="p">,</span>
        <span class="s">"x64_windows_msvc"</span><span class="p">,</span>
        <span class="s">"x64_windows"</span><span class="p">,</span>
     <span class="c1">// +  "aarch64",</span>
    <span class="p">]</span>
    <span class="k">for</span> <span class="n">cpu</span> <span class="n">in</span> <span class="n">cpu_configs</span><span class="o">:</span>
        <span class="n">native</span><span class="p">.</span><span class="n">config_setting</span><span class="p">(</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">//arm64没有pause指令</span>
<span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">ccVaJFns</span><span class="p">.</span><span class="n">s</span><span class="o">:</span><span class="mi">15314</span><span class="o">:</span> <span class="n">Error</span><span class="o">:</span> <span class="n">unknown</span> <span class="n">mnemonic</span> <span class="err">`</span><span class="n">pause</span><span class="err">'</span> <span class="o">--</span> <span class="err">`</span><span class="n">pause</span><span class="err">'</span>
<span class="c1">//解决： pause -&gt; yield</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="rouge-code"><pre><span class="mi">1</span><span class="o">&gt;</span> <span class="err">遇到编译错误</span>
<span class="n">ERROR</span><span class="o">:</span> <span class="o">/</span><span class="n">root</span><span class="o">/</span><span class="p">.</span><span class="n">cache</span><span class="o">/</span><span class="n">bazel</span><span class="o">/</span><span class="n">_bazel_root</span><span class="o">/</span><span class="mf">7e9</span><span class="n">b6806b9417f00ba913f77eac102ca</span><span class="o">/</span><span class="n">external</span><span class="o">/</span><span class="n">rules_foreign_cc</span><span class="o">/</span><span class="n">toolchains</span><span class="o">/</span><span class="n">BUILD</span><span class="p">.</span><span class="n">bazel</span><span class="o">:</span><span class="mi">77</span><span class="o">:</span><span class="mi">22</span><span class="o">:</span> <span class="err">@</span><span class="n">platforms</span><span class="c1">//os:windows is not a valid configuration key for @rules_foreign_cc//toolchains:built_make</span>
    
<span class="c1">//直接注释解决： 看这个暴力，非常有用</span>
    <span class="c1">//rules_foreign_cc/toolchains/BUILD.bazel:77</span>
<span class="n">native_tool_toolchain</span><span class="p">(</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">"built_make"</span><span class="p">,</span>
    <span class="n">path</span> <span class="o">=</span> <span class="n">select</span><span class="p">({</span>
        <span class="s">"//conditions:default"</span><span class="o">:</span> <span class="s">"$(execpath :make_tool)/bin/make"</span><span class="p">,</span>
    <span class="p">}),</span>
    <span class="n">target</span> <span class="o">=</span> <span class="s">":make_tool"</span><span class="p">,</span>
<span class="p">)</span>
    
 <span class="c1">//rules_foreign_cc/foreign_cc/private/framework/platform.bzl:33</span>
 <span class="mi">33</span> <span class="n">def</span> <span class="n">framework_platform_info</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">"platform_info"</span><span class="p">)</span><span class="o">:</span>
 <span class="mi">34</span>     <span class="s">"""Define a target containing platform information used in the foreign_cc framework"""</span>
 <span class="mi">35</span>     <span class="n">_framework_platform_info</span><span class="p">(</span>
 <span class="mi">36</span>         <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="p">,</span>
 <span class="mi">37</span>         <span class="n">os</span> <span class="o">=</span> <span class="n">select</span><span class="p">({</span>
 <span class="mi">38</span>             <span class="s">"//conditions:default"</span><span class="o">:</span> <span class="s">"unknown"</span><span class="p">,</span>
 <span class="mi">39</span>         <span class="p">}),</span>
 <span class="mi">40</span>         <span class="n">visibility</span> <span class="o">=</span> <span class="p">[</span><span class="s">"//visibility:public"</span><span class="p">],</span>
 <span class="mi">41</span>     <span class="p">)</span>
    
<span class="c1">//参考解决，实际没用</span>
    <span class="n">https</span><span class="o">:</span><span class="c1">//docs.bazel.build/versions/main/platforms-intro.html</span>
<span class="n">BUILD</span><span class="err">文件里指定编译目标，</span><span class="n">platforms</span><span class="p">,</span> <span class="n">cpus</span><span class="o">:</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//docs.bazel.build/versions/main/configurable-attributes.html</span>
<span class="n">Example</span><span class="o">:</span> <span class="n">Constraint</span> <span class="n">Values</span>
<span class="n">platform</span><span class="p">(</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">"linux_x86"</span><span class="p">,</span>
    <span class="n">constraint_values</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s">"@platforms//os:linux"</span><span class="p">,</span>
        <span class="s">"@platforms//cpu:x86_64"</span><span class="p">,</span>
    <span class="p">],</span>
<span class="p">)</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//blog.csdn.net/don_chiang709/article/details/105727621</span>
	<span class="n">wget</span> <span class="o">-</span><span class="n">c</span> <span class="n">https</span><span class="o">:</span><span class="c1">//github.com/bazelbuild/bazel/releases/download/3.0.0/bazel-3.0.0-installer-linux-x86_64.sh</span>
    <span class="n">chmod</span> <span class="o">+</span><span class="n">x</span> <span class="n">bazel</span><span class="o">-</span><span class="mi">3</span><span class="p">.</span><span class="mi">0</span><span class="p">.</span><span class="mi">0</span><span class="o">-</span><span class="n">installer</span><span class="o">-</span><span class="n">linux</span><span class="o">-</span><span class="n">x86_64</span><span class="p">.</span><span class="n">sh</span> 
    <span class="p">.</span><span class="o">/</span><span class="n">bazel</span><span class="o">-</span><span class="mi">3</span><span class="p">.</span><span class="mi">0</span><span class="p">.</span><span class="mi">0</span><span class="o">-</span><span class="n">installer</span><span class="o">-</span><span class="n">linux</span><span class="o">-</span><span class="n">x86_64</span><span class="p">.</span><span class="n">sh</span> <span class="o">--</span><span class="n">user</span>
        
<span class="n">https</span><span class="o">:</span><span class="c1">//john-millikin.com/bazel-school/toolchains</span>
<span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:cpu</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:arm</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:ppc</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:s390x</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:x86_32</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:x86_64</span>

<span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:os</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:freebsd</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:linux</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:osx</span>
    <span class="err">@</span><span class="n">bazel_tools</span><span class="c1">//platforms:windows</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">//clean:</span>
<span class="n">bazel</span> <span class="n">clean</span> <span class="o">--</span><span class="n">expunge</span>
<span class="c1">//编译顺序：</span>
    <span class="n">base</span> <span class="o">-&gt;</span> <span class="n">base</span><span class="o">-</span><span class="n">test</span> <span class="o">-&gt;</span> <span class="n">ghost</span> <span class="o">-&gt;</span> <span class="n">shared</span><span class="o">-&gt;</span> <span class="n">bazel</span> <span class="n">build</span> <span class="p">...</span> <span class="c1">//编译所有的</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">//bpf_skeleton -&gt; 定义bpf/bpf.bzl中</span>
    <span class="n">http</span><span class="o">:</span><span class="c1">//manpages.ubuntu.com/manpages/focal/man8/bpftool-gen.8.html</span>
<span class="o">-&gt;</span> <span class="n">bpftools</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">//[Bazel]自定义工具链	</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//cloud.tencent.com/developer/article/1677379</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">//bazel toolchain bazel 工具链</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//kekxv.github.io/2021/08/06/bazel%20toolchain%2001/</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="rouge-code"><pre><span class="c1">//查看本地编译配置</span>
<span class="n">https</span><span class="o">:</span><span class="c1">//www.coder.work/article/2786053</span>
<span class="err">`</span><span class="n">bazel</span> <span class="n">info</span> <span class="n">output_base</span><span class="err">`</span><span class="o">/</span><span class="n">external</span><span class="o">/</span><span class="n">local_config_cc</span>
 <span class="o">---------------</span>
<span class="p">[</span><span class="n">root</span><span class="err">@</span><span class="n">localhost</span> <span class="n">ghost</span><span class="o">-</span><span class="n">userspace</span><span class="p">]</span><span class="err">#</span> <span class="n">ll</span> <span class="err">`</span><span class="n">bazel</span>  <span class="n">info</span> <span class="n">output_base</span><span class="err">`</span><span class="o">/</span><span class="n">external</span><span class="o">/</span><span class="n">local_config_cc</span>
<span class="n">total</span> <span class="mi">32</span><span class="n">K</span>
<span class="n">lrwxrwxrwx</span><span class="p">.</span> <span class="mi">1</span> <span class="n">root</span> <span class="n">root</span>  <span class="mi">126</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">armeabi_cc_toolchain_config</span><span class="p">.</span><span class="n">bzl</span> <span class="o">-&gt;</span> <span class="o">/</span><span class="n">root</span><span class="o">/</span><span class="p">.</span><span class="n">cache</span><span class="o">/</span><span class="n">bazel</span><span class="o">/</span><span class="n">_bazel_root</span><span class="o">/</span><span class="mi">7433</span><span class="n">de7b30d6d58a288c26dfb16d43d1</span><span class="o">/</span><span class="n">external</span><span class="o">/</span><span class="n">bazel_tools</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">cpp</span><span class="o">/</span><span class="n">armeabi_cc_toolchain_config</span><span class="p">.</span><span class="n">bzl</span>
<span class="o">-</span><span class="n">rwxr</span><span class="o">-</span><span class="n">xr</span><span class="o">-</span><span class="n">x</span><span class="p">.</span> <span class="mi">1</span> <span class="n">root</span> <span class="n">root</span> <span class="mi">4</span><span class="p">.</span><span class="mi">4</span><span class="n">K</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">BUILD</span>
<span class="o">-</span><span class="n">rwxr</span><span class="o">-</span><span class="n">xr</span><span class="o">-</span><span class="n">x</span><span class="p">.</span> <span class="mi">1</span> <span class="n">root</span> <span class="n">root</span>  <span class="mi">552</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">builtin_include_directory_paths</span>
<span class="n">lrwxrwxrwx</span><span class="p">.</span> <span class="mi">1</span> <span class="n">root</span> <span class="n">root</span>  <span class="mi">123</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">cc_toolchain_config</span><span class="p">.</span><span class="n">bzl</span> <span class="o">-&gt;</span> <span class="o">/</span><span class="n">root</span><span class="o">/</span><span class="p">.</span><span class="n">cache</span><span class="o">/</span><span class="n">bazel</span><span class="o">/</span><span class="n">_bazel_root</span><span class="o">/</span><span class="mi">7433</span><span class="n">de7b30d6d58a288c26dfb16d43d1</span><span class="o">/</span><span class="n">external</span><span class="o">/</span><span class="n">bazel_tools</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">cpp</span><span class="o">/</span><span class="n">unix_cc_toolchain_config</span><span class="p">.</span><span class="n">bzl</span>
<span class="o">-</span><span class="n">rwxr</span><span class="o">-</span><span class="n">xr</span><span class="o">-</span><span class="n">x</span><span class="p">.</span> <span class="mi">1</span> <span class="n">root</span> <span class="n">root</span>  <span class="mi">739</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">cc_wrapper</span><span class="p">.</span><span class="n">sh</span>
<span class="n">drwxr</span><span class="o">-</span><span class="n">xr</span><span class="o">-</span><span class="n">x</span><span class="p">.</span> <span class="mi">3</span> <span class="n">root</span> <span class="n">root</span> <span class="mi">4</span><span class="p">.</span><span class="mi">0</span><span class="n">K</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">tools</span>
<span class="o">-</span><span class="n">rw</span><span class="o">-</span><span class="n">r</span><span class="o">--</span><span class="n">r</span><span class="o">--</span><span class="p">.</span> <span class="mi">1</span> <span class="n">root</span> <span class="n">root</span>  <span class="mi">111</span> <span class="n">Nov</span> <span class="mi">19</span> <span class="mi">10</span><span class="o">:</span><span class="mo">06</span> <span class="n">WORKSPACE</span>

</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="c1">//rules_foreign_cc支持的cmake、make、ninja版本</span>
<span class="c1">//rules_foreign_cc/foreign_cc/repositories.bzl</span>
<span class="n">register_default_tools</span> <span class="o">=</span> <span class="n">True</span><span class="p">,</span>
<span class="n">cmake_version</span> <span class="o">=</span> <span class="s">"3.21.2"</span><span class="p">,</span>
<span class="n">make_version</span> <span class="o">=</span> <span class="s">"4.3"</span><span class="p">,</span>
<span class="n">ninja_version</span> <span class="o">=</span> <span class="s">"1.10.2"</span><span class="p">,</span>

<span class="n">cmake</span> <span class="mi">3</span><span class="p">.</span><span class="mi">19</span><span class="p">.</span><span class="mi">2</span>
<span class="n">make</span> <span class="mi">4</span><span class="p">.</span><span class="mi">3</span>
<span class="n">ninja_build</span> <span class="mi">1</span><span class="p">.</span><span class="mi">8</span><span class="p">.</span><span class="mi">2</span>
</pre></td></tr></tbody></table></code></div></div>

<p>编译所有的ghost-userspace的二进制：</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre>
<span class="p">[</span><span class="n">root</span><span class="err">@</span><span class="n">localhost</span> <span class="n">ghost</span><span class="o">-</span><span class="n">userspace</span><span class="p">]</span><span class="err">#</span> <span class="n">bazel</span> <span class="n">build</span> <span class="p">...</span>
<span class="n">INFO</span><span class="o">:</span> <span class="n">Analyzed</span> <span class="mi">62</span> <span class="n">targets</span> <span class="p">(</span><span class="mi">17</span> <span class="n">packages</span> <span class="n">loaded</span><span class="p">,</span> <span class="mi">1505</span> <span class="n">targets</span> <span class="n">configured</span><span class="p">).</span>
<span class="n">INFO</span><span class="o">:</span> <span class="n">Found</span> <span class="mi">62</span> <span class="n">targets</span><span class="p">...</span>
<span class="n">INFO</span><span class="o">:</span> <span class="n">Elapsed</span> <span class="n">time</span><span class="o">:</span> <span class="mi">48</span><span class="p">.</span><span class="mi">090</span><span class="n">s</span><span class="p">,</span> <span class="n">Critical</span> <span class="n">Path</span><span class="o">:</span> <span class="mi">15</span><span class="p">.</span><span class="mi">80</span><span class="n">s</span>
<span class="n">INFO</span><span class="o">:</span> <span class="mi">531</span> <span class="n">processes</span><span class="o">:</span> <span class="mi">531</span> <span class="n">linux</span><span class="o">-</span><span class="n">sandbox</span><span class="p">.</span>
<span class="n">INFO</span><span class="o">:</span> <span class="n">Build</span> <span class="n">completed</span> <span class="n">successfully</span><span class="p">,</span> <span class="mi">798</span> <span class="n">total</span> <span class="n">actions</span>
</pre></td></tr></tbody></table></code></div></div>

<h4 id="4-思考"><span class="me-2">4&gt; 思考</span><a href="#4-思考" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<h5 id="1现在是cpu的进程调度放到用户态这个有什么优势-还能继续做些什么"><span class="me-2">1、现在是CPU的进程调度放到用户态，这个有什么优势？ 还能继续做些什么？</span><a href="#1现在是cpu的进程调度放到用户态这个有什么优势-还能继续做些什么" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<h5 id="2能结合反馈式智能调度吗"><span class="me-2">2、能结合反馈式智能调度吗？</span><a href="#2能结合反馈式智能调度吗" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<h5 id="3进程-资源调度"><span class="me-2">3、进程&lt;—-&gt;资源调度？</span><a href="#3进程-资源调度" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<h5 id="4cfs调度器的参数是全局的-如果有资源隔离的话可以将cfs也可以按照隔离域独立设置参数吗"><span class="me-2">4、CFS调度器的参数是全局的？ 如果有资源隔离的话，可以将CFS也可以按照隔离域独立设置参数吗？</span><a href="#4cfs调度器的参数是全局的-如果有资源隔离的话可以将cfs也可以按照隔离域独立设置参数吗" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<h5 id="5内核调度器中bpf的拓展-那内存部分ebpf可以有所作为吗"><span class="me-2">5、内核调度器中bpf的拓展, 那内存部分ebpf可以有所作为吗？</span><a href="#5内核调度器中bpf的拓展-那内存部分ebpf可以有所作为吗" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h5>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//www.ebpf.top/post/cfs_scheduler_bpf/  cfs的bpf优化.</span>

<span class="n">https</span><span class="o">:</span><span class="c1">//jirnal.com/train-of-ebpf-in-cpu-scheduler/</span>
<span class="n">TRAIN</span> <span class="n">OF</span> <span class="n">EBPF</span> <span class="n">IN</span> <span class="n">CPU</span> <span class="n">SCHEDULER</span>
<span class="n">eBPF</span> <span class="n">has</span> <span class="n">been</span> <span class="n">frail</span> <span class="n">broadly</span> <span class="n">in</span> <span class="n">efficiency</span> <span class="n">profiling</span> <span class="n">and</span> <span class="n">monitoring</span><span class="p">.</span> <span class="n">In</span> <span class="n">this</span> <span class="n">affirm</span><span class="p">,</span> <span class="n">I</span> <span class="n">am</span> <span class="n">going</span> <span class="n">to</span> <span class="n">exclaim</span> <span class="n">a</span> <span class="n">location</span> <span class="n">of</span> <span class="n">eBPF</span> <span class="n">positive</span> <span class="n">aspects</span> <span class="n">that</span> <span class="n">aid</span> <span class="n">show</span> <span class="n">screen</span> <span class="n">and</span> <span class="n">beef</span> <span class="n">up</span> <span class="n">cpu</span> <span class="n">scheduling</span> <span class="n">performances</span><span class="p">.</span> <span class="n">These</span> <span class="n">positive</span> <span class="n">aspects</span> <span class="n">consist</span> <span class="n">of</span><span class="o">:</span>

<span class="n">Profiling</span> <span class="n">scheduling</span> <span class="n">latencies</span><span class="p">.</span> <span class="n">I</span> <span class="n">am</span> <span class="n">going</span> <span class="n">to</span> <span class="n">focus</span> <span class="n">on</span> <span class="n">an</span> <span class="n">software</span> <span class="n">program</span> <span class="n">of</span> <span class="n">eBPF</span> <span class="n">to</span> <span class="n">amass</span> <span class="n">scheduling</span> <span class="n">latency</span> <span class="n">stats</span><span class="p">.</span>

<span class="n">Profiling</span> <span class="n">useful</span> <span class="n">resource</span> <span class="n">effectivity</span><span class="p">.</span> <span class="n">For</span> <span class="n">background</span><span class="p">,</span> <span class="n">I</span> <span class="n">am</span> <span class="n">going</span> <span class="n">to</span> <span class="n">first</span> <span class="n">introduce</span> <span class="n">the</span> <span class="n">scheduler</span> <span class="n">characteristic</span> <span class="n">core</span> <span class="n">scheduling</span> <span class="n">which</span> <span class="n">is</span> <span class="n">developed</span> <span class="k">for</span> <span class="n">mitigating</span> <span class="n">L1TF</span> <span class="n">cpu</span> <span class="n">vulnerability</span><span class="p">.</span> <span class="n">Then</span> <span class="n">I</span> <span class="n">am</span> <span class="n">going</span> <span class="n">to</span> <span class="n">introduce</span> <span class="n">the</span> <span class="n">eBPF</span> <span class="n">characteristic</span> <span class="n">ksym</span> <span class="n">which</span> <span class="n">enables</span> <span class="n">this</span> <span class="n">software</span> <span class="n">program</span> <span class="n">and</span> <span class="n">affirm</span> <span class="n">how</span> <span class="n">eBPF</span> <span class="n">can</span> <span class="n">support</span> <span class="n">yarn</span> <span class="n">the</span> <span class="n">forced</span> <span class="n">lazy</span> <span class="n">time</span><span class="p">,</span> <span class="n">a</span> <span class="n">invent</span> <span class="n">of</span> <span class="n">cpu</span> <span class="n">usage</span> <span class="n">inefficiency</span> <span class="n">caused</span> <span class="n">by</span> <span class="n">core</span> <span class="n">scheduling</span><span class="p">.</span>

<span class="n">The</span> <span class="n">third</span> <span class="n">software</span> <span class="n">program</span> <span class="n">of</span> <span class="n">eBPF</span> <span class="n">is</span> <span class="n">to</span> <span class="n">encourage</span> <span class="n">userspace</span> <span class="n">scheduling</span><span class="p">.</span> <span class="n">ghOSt</span> <span class="n">is</span> <span class="n">a</span> <span class="n">framework</span> <span class="n">open</span> <span class="n">sourced</span> <span class="n">by</span> <span class="n">Google</span> <span class="n">to</span> <span class="n">enable</span> <span class="n">overall</span><span class="o">-</span><span class="n">goal</span> <span class="n">delegation</span> <span class="n">of</span> <span class="n">scheduling</span> <span class="n">protection</span> <span class="n">to</span> <span class="n">userspace</span> <span class="n">processes</span> <span class="n">in</span> <span class="n">a</span> <span class="n">Linux</span> <span class="n">ambiance</span><span class="p">.</span> <span class="n">ghOSt</span> <span class="n">uses</span> <span class="n">BPF</span> <span class="n">acceleration</span> <span class="k">for</span> <span class="n">defense</span> <span class="n">actions</span> <span class="n">that</span> <span class="n">deserve</span> <span class="n">to</span> <span class="n">occur</span> <span class="n">closer</span> <span class="n">to</span> <span class="n">scheduling</span> <span class="n">edges</span><span class="p">.</span> <span class="n">We</span> <span class="n">use</span> <span class="n">this</span> <span class="n">to</span> <span class="n">maximize</span> <span class="n">CPU</span> <span class="n">utilization</span> <span class="p">(</span><span class="n">pick_next_task</span><span class="p">),</span> <span class="n">decrease</span> <span class="n">jitter</span> <span class="p">(</span><span class="n">task_tick</span> <span class="n">elision</span><span class="p">)</span> <span class="n">and</span> <span class="n">preserve</span> <span class="n">watch</span> <span class="n">over</span> <span class="n">tail</span> <span class="n">latency</span> <span class="p">(</span><span class="n">select_task_rq</span> <span class="n">on</span> <span class="n">wakeup</span><span class="p">).</span> <span class="n">We</span><span class="err">’</span><span class="n">re</span> <span class="n">also</span> <span class="n">experimenting</span> <span class="n">with</span> <span class="n">BPF</span> <span class="n">to</span> <span class="n">place</span> <span class="n">in</span> <span class="n">pressure</span> <span class="n">a</span> <span class="n">scaled</span><span class="o">-</span><span class="n">down</span> <span class="n">variant</span> <span class="n">of</span> <span class="n">the</span> <span class="n">scheduling</span> <span class="n">protection</span> <span class="k">while</span> <span class="n">upgrading</span> <span class="n">the</span> <span class="n">principle</span> <span class="n">userspace</span> <span class="n">ghOSt</span> <span class="n">agent</span><span class="p">.</span>
</pre></td></tr></tbody></table></code></div></div>

<h3 id="三扩展"><span class="me-2">三、扩展</span><a href="#三扩展" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>
<h4 id="1调度激活机制"><span class="me-2">1&gt;、调度激活机制</span><a href="#1调度激活机制" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//www.codenong.com/cs105549070/ 调度激活机制</span>
<span class="n">upcall</span><span class="err">具体做了什么？</span>

<span class="err">内核发现用户进程的一个线程被</span><span class="n">block</span><span class="err">了（比如调用了一个被</span><span class="n">block</span><span class="err">的</span><span class="n">system</span> <span class="n">call</span><span class="err">，或者发生了缺页异常。</span>
<span class="err">内核通过进程的运行时线程被</span><span class="n">block</span><span class="err">（还会告知被</span><span class="n">block</span><span class="err">的线程的详细信息），这个就是</span><span class="n">upcall</span>
<span class="err">进程的运行时系统接收到内核发来的消息，得知自己的线程被</span><span class="n">block</span>
<span class="err">运行时系统先将当前线程标识为</span><span class="n">block</span><span class="err">（会保存在线程表</span><span class="o">-</span><span class="kr">thread</span> <span class="n">table</span><span class="err">）</span>
<span class="err">运行时系统从当前线程表（</span><span class="kr">thread</span> <span class="n">table</span><span class="err">）选择一个</span><span class="n">ready</span><span class="err">的线程进行运行</span>
<span class="err">至此已经完成了当一个进程内的一个用户线程被</span><span class="n">block</span><span class="p">(</span><span class="err">比如发生缺页异常）时，不会导致整个进程被</span><span class="n">block</span><span class="err">，这个进程的内的其他用户线程还可以继续运行（类似于内核线程）。</span>

<span class="err">当内核发现之前被</span><span class="n">block</span><span class="err">的线程可以</span><span class="n">run</span><span class="err">了，同样会通过</span><span class="n">upcall</span><span class="err">通知运行时系统，运行时系统要么马上运行该线程，要么把该线程标志位</span><span class="n">ready</span><span class="err">放入线程表。</span>
</pre></td></tr></tbody></table></code></div></div>

<h4 id="2基于网络栈的用户态定制调度"><span class="me-2">2&gt;、基于网络栈的用户态定制调度</span><a href="#2基于网络栈的用户态定制调度" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<p>Syrup: User-Defined Scheduling Across the Stack</p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">http:</span><span class="c1">//stanford.edu/~kkaffes/papers/syrup.pdf</span>
</pre></td></tr></tbody></table></code></div></div>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//zhuanlan.zhihu.com/p/464560315</span>
</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/Syrup定制调度.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/Syrup定制调度.png" alt="img" class="lazyload" data-proofer-ignore></a></p>

<h3 id="3-fast-preemt快速抢占"><span class="me-2">3&gt; fast preemt快速抢占</span><a href="#3-fast-preemt快速抢占" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<pre><code class="language-C">https://ubuntu.com/blog/industrial-embedded-systems-ii  Low latency Linux kernel for industrial embedded systems – Part II
</code></pre>

<h3 id="4-轻量级线程lwt"><span class="me-2">4&gt; 轻量级线程LWT</span><a href="#4-轻量级线程lwt" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<h3 id="ps"><span class="me-2">PS：</span><a href="#ps" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h3>

<h4 id="1-linux内核进程状态机转化"><span class="me-2">1&gt; linux内核进程状态机转化</span><a href="#1-linux内核进程状态机转化" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nl">https:</span><span class="c1">//blog.51cto.com/ciellee/3411615 操作系统进程状态和状态转换详解	</span>
</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转化图1.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转化图1.png" alt="image-20220304165958542" class="lazyload" data-proofer-ignore></a></p>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转化图2.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转化图2.png" alt="image-20220304174351554" class="lazyload" data-proofer-ignore></a></p>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="nl">shenango:</span>
<span class="n">Process</span> <span class="n">Context</span> <span class="n">Identifiers</span> <span class="p">(</span><span class="n">PCIDs</span><span class="p">)</span> <span class="n">allow</span> <span class="n">page</span> <span class="n">tables</span> <span class="n">to</span> <span class="n">be</span> <span class="n">swapped</span> <span class="n">without</span> <span class="n">flushing</span> <span class="n">the</span> <span class="n">TLB</span>
</pre></td></tr></tbody></table></code></div></div>

<p><a href="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转移+func.png" class="popup img-link "><img data-src="/posts/./images/Customized-Scheduler-In-Userspace/进程状态转移+func.png" alt="image-20220412141353560" class="lazyload" data-proofer-ignore></a></p>

<h4 id="2-可监控的资源"><span class="me-2">2&gt; 可监控的资源</span><a href="#2-可监控的资源" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="table-wrapper"><table>
  <thead>
    <tr>
      <th>监控类型</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>end-to-end  latency</td>
    </tr>
    <tr>
      <td>schedule  latency</td>
    </tr>
    <tr>
      <td>Memory  Bandwidth</td>
    </tr>
    <tr>
      <td>LLC miss rate</td>
    </tr>
    <tr>
      <td>Network  Bandwidth</td>
    </tr>
    <tr>
      <td>Disk Bandwidth</td>
    </tr>
  </tbody>
</table></div>

<p>应用特征不同，资源监控（cpu、内存、磁盘、网络）使用率和策略应该是定制化的。或者是性能特征来作为资源调度的依据</p>

<h4 id="3-现有混布与ghost方案对比"><span class="me-2">3&gt; 现有混布与ghost方案对比</span><a href="#3-现有混布与ghost方案对比" class="anchor text-muted"><i class="fas fa-hashtag"></i></a></h4>

<div class="language-c highlighter-rouge"><div class="code-header">
        <span data-label-text="C"><i class="fas fa-code fa-fw small"></i></span>
      <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div><div class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="rouge-code"><pre><span class="mi">1</span><span class="err">、</span><span class="n">QOS</span><span class="err">混部方案当前的缺点及问题</span>
 <span class="n">QOS</span><span class="err">混布方案是在内核</span><span class="n">CFS</span><span class="err">增加定制，由于</span><span class="n">CFS</span><span class="err">本身的复杂度很高，导致新增调度策略实现难度大，扩展性弱。</span>
     
<span class="mi">2</span><span class="err">、</span><span class="n">Ghost</span><span class="err">方案的主要特点，及对比混部的优势</span>
  <span class="mi">1</span><span class="o">&gt;</span> <span class="err">可定制的调度实现框架，可以结合场景特点来设计满足不同延迟、吞吐量要求的调度器。</span>
  <span class="mi">2</span><span class="o">&gt;</span> <span class="err">在用户态设计调度策略，独立于内核的</span><span class="n">CFS</span><span class="err">调度器，降低调度器实现难度，扩展性强。</span>
  <span class="mi">3</span><span class="o">&gt;</span> <span class="n">ghost</span><span class="err">可以灵活部署，可以支持动态升级回滚，不需要重新编译内核、重启。</span>
 <span class="n">ghost</span><span class="err">就是提供了调度器的设计、实现、部署的一种新范式，与传统的调度器设计有很大区别。</span>
    
<span class="mi">3</span><span class="err">、</span><span class="n">Ghost</span><span class="err">框架灵活性等特点，是否有解决其他问题的潜力</span>
   <span class="err">有解决其他问题的潜力；</span>
    <span class="mi">1</span><span class="o">&gt;</span> <span class="n">ghost</span><span class="err">设计之初是在</span><span class="n">google</span><span class="err">的数据中心使用，实现在高吞吐量的前提实现低尾时延；</span>
    <span class="mi">2</span><span class="o">&gt;</span> <span class="err">由于具有灵活的可定制调度策略的特性，混布场景自然也可以使用</span>
    <span class="mi">3</span><span class="o">&gt;</span> <span class="err">异构设备场景下的任务调度分发或许也是个很好的场景。</span>
</pre></td></tr></tbody></table></code></div></div>


</div>

<div class="post-tail-wrapper text-muted">

  <!-- categories -->
  
  <div class="post-meta mb-3">
    <i class="far fa-folder-open fa-fw me-1"></i>
    
      <a href='/categories/linux/'>linux</a>,
      <a href='/categories/scheduler/'>Scheduler</a>
  </div>
  
  <!-- post.html -->

  <!-- tags -->
  
  <div class="post-tags">
    <i class="fa fa-tags fa-fw me-1"></i>
      
      <a href="/tags/scheduler/"
          class="post-tag no-text-decoration" >Scheduler</a>
      
  </div>
  
  
  <div class="post-tail-bottom
    d-flex justify-content-between align-items-center mt-3 pt-5 pb-2">
    <div class="license-wrapper">

      

        

        This post is licensed under 
        <a href="https://creativecommons.org/licenses/by/4.0/">
          CC BY 4.0
        </a>
         by the author.

      
    </div>

    <!-- Post sharing snippet -->

<div class="share-wrapper">
  <span class="share-label text-muted me-1">Share</span>
  <span class="share-icons">
    
    
    

    
      
      <a
        href="https://twitter.com/intent/tweet?text=Customized%20Scheduler%20-%20google%20Ghost%20-%20Cheng%20Luo&url=https%3A%2F%2Fluochenglcs.github.io%2Fposts%2FCustomized-Scheduler-In-Userspace%2F"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="Twitter"
        target="_blank"
        rel="noopener"
        aria-label="Twitter"
      >
        <i class="fa-fw fab fa-twitter"></i>
      </a>
    
      
      <a
        href="https://www.facebook.com/sharer/sharer.php?title=Customized%20Scheduler%20-%20google%20Ghost%20-%20Cheng%20Luo&u=https%3A%2F%2Fluochenglcs.github.io%2Fposts%2FCustomized-Scheduler-In-Userspace%2F"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="Facebook"
        target="_blank"
        rel="noopener"
        aria-label="Facebook"
      >
        <i class="fa-fw fab fa-facebook-square"></i>
      </a>
    
      
      <a
        href="https://t.me/share/url?url=https%3A%2F%2Fluochenglcs.github.io%2Fposts%2FCustomized-Scheduler-In-Userspace%2F&text=Customized%20Scheduler%20-%20google%20Ghost%20-%20Cheng%20Luo"
        data-bs-toggle="tooltip"
        data-bs-placement="top"
        title="Telegram"
        target="_blank"
        rel="noopener"
        aria-label="Telegram"
      >
        <i class="fa-fw fab fa-telegram"></i>
      </a>
    

    <i
      id="copy-link"
      class="fa-fw fas fa-link small"
      data-bs-toggle="tooltip"
      data-bs-placement="top"
      title="Copy link"
      data-title-succeed="Link copied successfully!"
    >
    </i>
  </span>
</div>


  </div><!-- .post-tail-bottom -->

</div><!-- div.post-tail-wrapper -->


      
    
      
    </div>
  </div>
  <!-- #core-wrapper -->

  <!-- panel -->
  <div id="panel-wrapper" class="col-xl-3 ps-2 text-muted">
    <div class="access">
      <!-- Get the last 5 posts from lastmod list. -->














  <div id="access-lastmod" class="post">
    <div class="panel-heading">Recently Updated</div>
    <ul class="post-content list-unstyled ps-0 pb-1 ms-1 mt-2">
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/%E5%86%85%E5%AD%98%E6%95%B0%E6%8D%AE%E5%BA%93/">MMDB (memory database)</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/userspace-rcu%E9%94%81/">userspace rcu lock</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/Real-time-operating-system/">Realtime Operating System</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/HUNDSUN-LightOS/">Hundsun Lightos</a>
        </li>
      
        
        
        
        <li class="text-truncate lh-lg">
          <a href="/posts/linux-struct-folio/">struct folio</a>
        </li>
      
    </ul>
  </div>
  <!-- #access-lastmod -->


      <!-- The trending tags list -->















  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        



  <div id="access-tags">
    <div class="panel-heading">Trending Tags</div>
    <div class="d-flex flex-wrap mt-3 mb-1 me-3">
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/lock/">lock</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/libumem/">libumem</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/mm/">mm</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/autonuma/">autonuma</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bfs/">BFS</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bus/">Bus</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/caladan/">caladan</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/damon/">damon</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/deep-learning/">Deep Learning</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/dma/">DMA</a>
      
    </div>
  </div>


    </div>

    
      
      



  <div id="toc-wrapper" class="ps-0 pe-4 mb-5">
    <div class="panel-heading ps-3 pt-2 mb-2">Contents</div>
    <nav id="toc"><ul><li>Customized Scheduler - google Ghost<ul><li><a href="#一鸿蒙调度liteos调度">一、鸿蒙调度/LiteOS调度</a></li><li><a href="#二ghost调度器">二、ghost调度器</a><ul><li><a href="#1-ghost-kernel如何调度切换">1&gt; ghost-kernel–如何调度切换</a><ul><li><a href="#1ghost-class-timerfd">1、ghost class timerfd</a></li><li><a href="#2kernelschedghostc">2、kernel/sched/ghost.c</a></li><li><a href="#3new-syscall--ghost-ghost_run">3、new syscall – ghost ghost_run</a></li><li><a href="#4bpf">4、bpf</a></li><li><a href="#5kernelschedghostfsc">5、kernel/sched/ghostfs.c</a><ul><li><a href="#1-提供enclave的创建"><strong>1 提供enclave的创建</strong></a></li><li><a href="#2-提供cpu_data"><strong>2 提供cpu_data</strong></a></li><li><a href="#3-提供status_word">3 提供status_word</a></li></ul></li><li><a href="#6-ghost调度分析">6、 ghost调度分析</a></li></ul></li><li><a href="#2-ghost-userspace">2&gt; ghost-userspace</a><ul><li><a href="#1-ebpf机制">1&gt; ebpf机制</a></li><li><a href="#简介">简介</a></li><li><a href="#bpf-map">bpf map</a></li><li><a href="#bpf-sec--bpf-hook">bpf SEC – bpf hook</a></li><li><a href="#bfp辅助函数">bfp辅助函数</a></li><li><a href="#2-用户态代码">2 用户态代码</a></li></ul></li><li><a href="#3-experiments">3&gt; experiments</a></li><li><a href="#3-ghost用户态工具的编译">3&gt; ghost用户态工具的编译</a><ul><li><a href="#1-bazel">1 bazel</a><ul><li><a href="#简介-1">简介</a></li><li><a href="#依赖">依赖</a></li><li><a href="#编译">编译</a></li></ul></li></ul></li><li><a href="#4-思考">4&gt; 思考</a><ul><li><a href="#1现在是cpu的进程调度放到用户态这个有什么优势-还能继续做些什么">1、现在是CPU的进程调度放到用户态，这个有什么优势？ 还能继续做些什么？</a></li><li><a href="#2能结合反馈式智能调度吗">2、能结合反馈式智能调度吗？</a></li><li><a href="#3进程-资源调度">3、进程&lt;—-&gt;资源调度？</a></li><li><a href="#4cfs调度器的参数是全局的-如果有资源隔离的话可以将cfs也可以按照隔离域独立设置参数吗">4、CFS调度器的参数是全局的？ 如果有资源隔离的话，可以将CFS也可以按照隔离域独立设置参数吗？</a></li><li><a href="#5内核调度器中bpf的拓展-那内存部分ebpf可以有所作为吗">5、内核调度器中bpf的拓展, 那内存部分ebpf可以有所作为吗？</a></li></ul></li></ul></li><li><a href="#三扩展">三、扩展</a><ul><li><a href="#1调度激活机制">1&gt;、调度激活机制</a></li><li><a href="#2基于网络栈的用户态定制调度">2&gt;、基于网络栈的用户态定制调度</a></li></ul></li><li><a href="#3-fast-preemt快速抢占">3&gt; fast preemt快速抢占</a></li><li><a href="#4-轻量级线程lwt">4&gt; 轻量级线程LWT</a></li><li><a href="#ps">PS：</a><ul><li><a href="#1-linux内核进程状态机转化">1&gt; linux内核进程状态机转化</a></li><li><a href="#2-可监控的资源">2&gt; 可监控的资源</a></li><li><a href="#3-现有混布与ghost方案对比">3&gt; 现有混布与ghost方案对比</a></li></ul></li></ul></li></ul></li></ul></nav>
  </div>
  


    
  </div>
</div>

<!-- tail -->

  <div class="row">
    <div id="tail-wrapper" class="col-12 col-lg-11 col-xl-9 px-3 pe-xl-4 mt-5">
      
        
        <!--
  Recommend the other 3 posts according to the tags and categories of the current post,
  if the number is not enough, use the other latest posts to supplement.
-->

<!-- The total size of related posts -->


<!-- An random integer that bigger than 0 -->


<!-- Equals to TAG_SCORE / {max_categories_hierarchy} -->








  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
    
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  






<!-- Fill with the other newlest posts -->





  <div id="related-posts" class="mb-2 mb-sm-4">
    <h3 class="pt-2 mb-4 ms-1" data-toc-skip>
      Further Reading
    </h3>
    <div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4 mb-4">
      
        
        
        <div class="col">
          <a href="/posts/linux-kcsan/" class="card post-preview h-100">
            <div class="card-body">
              <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class="small"
  data-ts="1599782400"
  data-df="ll"
  
>
  Sep 11, 2020
</em>

              <h4 class="pt-0 my-2" data-toc-skip>The Kernel Concurrency Sanitizer (KCSAN)</h4>
              <div class="text-muted small">
                <p>
                  





                  1 kcasan

2 使用

                </p>
              </div>
            </div>
          </a>
        </div>
      
        
        
        <div class="col">
          <a href="/posts/linux-kprobe/" class="card post-preview h-100">
            <div class="card-body">
              <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class="small"
  data-ts="1600041600"
  data-df="ll"
  
>
  Sep 14, 2020
</em>

              <h4 class="pt-0 my-2" data-toc-skip>Kernel Probe (kprobe)</h4>
              <div class="text-muted small">
                <p>
                  





                  1 kprobe

2 debugfs kprobe trace
kprobe trace 使用debugfs的trace功能
 p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS]  : Set a probe
 r[MAXACTIVE][:[GRP/]EVENT] [MOD:]SYM[+0] [FETCHARGS]  : Set a r...
                </p>
              </div>
            </div>
          </a>
        </div>
      
        
        
        <div class="col">
          <a href="/posts/linux-userfaultfd/" class="card post-preview h-100">
            <div class="card-body">
              <!--
  Date format snippet
  See: ${JS_ROOT}/utils/locale-dateime.js
-->





<em
  class="small"
  data-ts="1600905600"
  data-df="ll"
  
>
  Sep 24, 2020
</em>

              <h4 class="pt-0 my-2" data-toc-skip>Kernel userfaultfd</h4>
              <div class="text-muted small">
                <p>
                  





                  Userfault只支持匿名页，hugetlb、共享内存；

一、软件流程

1 初始化
调用__NR_userfaultfd syscall初始化
调用syscall初始化建立匿名inode文件，并初始化file-&amp;gt;private_data,并返回用户态文件fd。


  
    用户态：

    uffd = syscall(__NR_userfaultfd, O_CLOEXE...
                </p>
              </div>
            </div>
          </a>
        </div>
      
    </div>
    <!-- .card-deck -->
  </div>
  <!-- #related-posts -->


      
        
        <!-- Navigation buttons at the bottom of the post. -->

<div class="post-navigation d-flex justify-content-between">
  
    <a
      href="/posts/ROS2-For-Industrial-Control/"
      class="btn btn-outline-primary"
      prompt="Older"
    >
      <p>The Robot Operating System 2 (ROS2)</p>
    </a>
  

  
    <a
      href="/posts/linux-page_idle-bitmap/"
      class="btn btn-outline-primary"
      prompt="Newer"
    >
      <p>Kernel Page_Idle</p>
    </a>
  
</div>

      
        
        <!--  The comments switcher -->


      
    </div>
  </div>


        <!-- The Search results -->

<div id="search-result-wrapper" class="d-flex justify-content-center unloaded">
  <div class="col-11 post-content">
    <div id="search-hints">
      <!-- The trending tags list -->















  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        

  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
    
  
    
    
    
    
      
        
        



  <div id="access-tags">
    <div class="panel-heading">Trending Tags</div>
    <div class="d-flex flex-wrap mt-3 mb-1 me-3">
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/lock/">lock</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/libumem/">libumem</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/mm/">mm</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/autonuma/">autonuma</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bfs/">BFS</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/bus/">Bus</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/caladan/">caladan</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/damon/">damon</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/deep-learning/">Deep Learning</a>
      
        
        <a class="post-tag btn btn-outline-primary" href="/tags/dma/">DMA</a>
      
    </div>
  </div>


    </div>
    <div id="search-results" class="d-flex flex-wrap justify-content-center text-muted mt-3"></div>
  </div>
</div>

      </div>
    </div>

    <!-- The Footer -->

<footer>
  <div class="container px-lg-4">
    <div class="d-flex justify-content-center align-items-center text-muted mx-md-3">
      <p>Using the <a href="https://jekyllrb.com" target="_blank" rel="noopener">Jekyll</a> theme <a href="https://github.com/cotes2020/jekyll-theme-chirpy" target="_blank" rel="noopener">Chirpy</a>
      </p>

      <p>©
        2023
        <a href="https://gitee.com/luochenglcs">luochunsheng</a>.
        
          <span
            data-bs-toggle="tooltip"
            data-bs-placement="top"
            title="Except where otherwise noted, the blog posts on this site are licensed under the Creative Commons Attribution 4.0 International (CC BY 4.0) License by the author."
          >Some rights reserved.</span>
        
      </p>
    </div>
  </div>
</footer>


    <div id="mask"></div>

    <button id="back-to-top" aria-label="back-to-top" class="btn btn-lg btn-box-shadow">
      <i class="fas fa-angle-up"></i>
    </button>

    
      <div
        id="notification"
        class="toast"
        role="alert"
        aria-live="assertive"
        aria-atomic="true"
        data-bs-animation="true"
        data-bs-autohide="false"
      >
        <div class="toast-header">
          <button
            type="button"
            class="btn-close ms-auto"
            data-bs-dismiss="toast"
            aria-label="Close"
          ></button>
        </div>
        <div class="toast-body text-center pt-0">
          <p class="px-2 mb-3">A new version of content is available.</p>
          <button type="button" class="btn btn-primary" aria-label="Update">
            Update
          </button>
        </div>
      </div>
    

    <!-- JS selector for site. -->

<!-- commons -->



<!-- layout specified -->


  

  
    <!-- image lazy-loading & popup & clipboard -->
    
  















  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  

  
    

    

  



  <script src="https://cdn.jsdelivr.net/combine/npm/jquery@3.7.0/dist/jquery.min.js,npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js,npm/simple-jekyll-search@1.10.0/dest/simple-jekyll-search.min.js,npm/lazysizes@5.3.2/lazysizes.min.js,npm/magnific-popup@1.1.0/dist/jquery.magnific-popup.min.js,npm/clipboard@2.0.11/dist/clipboard.min.js,npm/dayjs@1.11.7/dayjs.min.js,npm/dayjs@1.11.7/locale/en.min.js,npm/dayjs@1.11.7/plugin/relativeTime.min.js,npm/dayjs@1.11.7/plugin/localizedFormat.min.js,npm/tocbot@4.21.0/dist/tocbot.min.js"></script>






<script defer src="/assets/js/dist/post.min.js"></script>






    

    <!--
  Jekyll Simple Search loader
  See: <https://github.com/christian-fei/Simple-Jekyll-Search>
-->





<script>
  /* Note: dependent library will be loaded in `js-selector.html` */
  SimpleJekyllSearch({
    searchInput: document.getElementById('search-input'),
    resultsContainer: document.getElementById('search-results'),
    json: '/assets/js/data/search.json',
    searchResultTemplate: '<div class="px-1 px-sm-2 px-lg-4 px-xl-0">  <a href="{url}">{title}</a>  <div class="post-meta d-flex flex-column flex-sm-row text-muted mt-1 mb-1">    {categories}    {tags}  </div>  <p>{snippet}</p></div>',
    noResultsText: '<p class="mt-5"></p>',
    templateMiddleware: function(prop, value, template) {
      if (prop === 'categories') {
        if (value === '') {
          return `${value}`;
        } else {
          return `<div class="me-sm-4"><i class="far fa-folder fa-fw"></i>${value}</div>`;
        }
      }

      if (prop === 'tags') {
        if (value === '') {
          return `${value}`;
        } else {
          return `<div><i class="fa fa-tag fa-fw"></i>${value}</div>`;
        }
      }
    }
  });
</script>

  </body>
</html>

